How to add the binary of a int with the binary of a string
Basically i want to be able to get a 32bit int and attach开发者_如何学运维 its binary to the binary of a string. E.g. (IM going to use 8bit instead of 32bit) i want 255 + hi 11111111 + 0110100001101001 = 111111110110100001101001 So the int holds its binary value,i dont care how it comes out i just want it to be able to send the data over a socket.
(This is all over websockets and the new sec-websocket-key's to stop hacking, if anyone just knows how to do the websocket handshake that would be just as nice)
Thankyou ! I have been trying on this for days and im not one to come to this type of website to get the answer
EDIT
Ive been ask to give more info so her is the full deal. I have connected to the user of a stream port, he has sent me headers now i need to reply to to complete the connection. The import data is
Sec-WebSocket-Key1: 4 @1 46546xW%0l 1 5"Random string following rules"(i will call this sk1)
Sec-WebSocket-Key2: 12998 5 Y3 1 .P00 "Random string following rules"(i will call this sk2)
^n:ds[4U "Random string following rules"(i will call this sk3)
1) int1 = compress the numbers into sk1 and divid them by the amount of spaces in sk1
2) int2 = compress the numbers into sk2 and divid them by the amount of spaces in sk2
3) fullapend = Add append the bytes of int2 to int1 then append the bytes in sk3
4) Finally MD5 digest fullapend
5) Send the final result to the host along with some other headers and if they match up the connection holds open
That is everything that needs to happen and i have not got a clue how to do it
Finished !
Well basic both answers was right and i would like to apologise if i seemed a bit rude, i didnt know the \x was a (something) meaning binary. but that worked a treat. Once i have the finished function to connect send etc... i will post it on here and else where for anyone else thats stuck, again thankyou !
Something like
struct.pack("!i%ds" % len(your_string), your_int, your_string)
should do pretty much what you want !
Is this what you're looking for? Don't know whether you're after a hexdigest or a digest, and I couldn't tell where the keys started and stopped, which is a shame as they are whitespace sensitive.
Also in your update you said "compress" numbers when I think you meant "concatenate". I think the resulting keys are supposed to be bigendian, which is what I've done.
>>> import struct
>>> def processKey(data):
... num = int("".join([x for x in data if x.isdigit()]))
... spaces = data.count(' ')
... return num / spaces
...
>>> key1 = '4 @1 46546xW%0l 1 5'
>>> key2 = '12998 5 Y3 1 .P00 '
>>> sk1 = processKey(key1)
>>> sk2 = processKey(key2)
>>> sk1
1036636503L
>>> sk2
259970620
>>> sk3 = "^n:ds[4U"
>>> fullappend = struct.pack('>ii%ds' % len(sk3),sk1,sk2,sk3)
>>> fullappend
'=\xc9\xd1W\x0f~\xd6<^n:ds[4U'
>>> len(fullappend)
16
>>> from hashlib import md5
>>> md5(fullappend).hexdigest()
'fd028b6b39ceb8e37f09b8e45556bbc4'
>>> md5(fullappend).digest()
'\xfd\x02\x8bk9\xce\xb8\xe3\x7f\t\xb8\xe4UV\xbb\xc4'
精彩评论