Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Binary string

Status
Not open for further replies.

stipey75

Programmer
May 6, 2005
2
IT
I am implementing a client server program.
Client side is written in C++ it sends to a phyton server packets.
For example the phyton buffer receive:

datarecv = 'i\001\017\000\001\000\334\005...'

Where every part between backslash is a byte.
My problem is to make phyton understand as a number the fields because some are bytes some are words and some Dword
for example i want that \001\ is read like an unsigned char (it means 1 in decimal notation) or \017\000 like a word (it means 15 in decimal notation!).
I know that there's the struct module to do this, but i wanted to know if there's a method with the built-in functions!!

Thanks a lot
Lucio
 
If you simply showed me the string, how would you explain to me how to recognize which should be interpreted as bytes and which should be read as byte pairs?

Also, is the string "\001" 4 bytes or one byte?

 
ok so \001 is a byte and every number between backslash is a byte.
I' ve made so to solve the problem:
##FOR A BYTE###############
def GET_BYTE(Buffer):

global ind

Byte = ord(Buffer[ind])
ind = ind + 1
return Byte
###########################

######FOR A WORD###########
def GET_WORD(Buffer):

global ind
FirstByte = ord(Buffer[ind])
SecondByte = ord(Buffer[ind+1])
Word = (SecondByte << 8) + FirstByte
ind = ind + 2
return Word
###########################

the same for a Dword...now i have to make the inverse so to put a byte or a word in the packet to send...
 
So, there's some layout that you know that tells whether the fields are bytes, words or double words. If it's a fixed format record, then it would be very easily done with the struct package.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top