[Tutor] Will someone please tell me how to read this?

Alan G alan.gauld at freenet.co.uk
Tue Aug 23 00:09:48 CEST 2005


> Is there a name for this type of number?  I'm assuming the \x means 
> it's hex, but then you have things like \x00h, and \x007p^.

It's a sequence of bytes and you've asked Python to print its 
string representation.  So where the bytes are unprintable 
ascii codes it shows you the hex, thus:

'\x05\x01\x03\x02\x05\xaf\xce\x04\x00

Those are all single bytes in hex

h

that byte has the ascii value for the letter 'h'
(ie. its 104 in decimal)

\x00\x00\x00\x01\x007\x00\x01\x00\x01\x00

More bytes as hex

7
p
^

The three ascii characters '7','p','^' 
(or 55,112,94 in decimal)

\x00\x00\x00\x00\x00\x00\x00\x01\x00

More hex bytes

7

'7' again

\x00\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00

more bytes as hex... you get the idea?

However that doesn't mean that those are really what the 
data represents. The first 4 bytes could be a 32 bit integer, 
or a short floating point value.

Similary the 7p^ sequence could be three bytes from within an 
integer too.

You really need to know whats being thrown at you down the socket 
otherwise you have very little hope of accurately decoding it.
And to decode it you need to use the struct module.

HTH,

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld


More information about the Tutor mailing list