Re: [Tutor] Struct the solution for Hex translation
Johan Geldenhuys [EMAIL PROTECTED] wrote On the struct module, How can I het the binary 1's and 0's of the Hex value? Let say I want to get the 8 bit value of '\xe2', can I use struct to convert that into binary code No, struct converts your data into a string of bytes and provides a way to view a string representation of those bytes. But you can use bitmasks to get at the individual bits. (See my Using the OS topic for a box on bitwise operators and use of bitmasks.) Basically you want to bitwise AND the data with a mask containing just a one in the 8th position (since 1 0 = 0 and 1 1 = 1) Thus 1000 = 80 hex. So: for n in range(125,130): ...print n,':',n0x80 ... 125 : 0 126 : 0 127 : 0 128 : 128 --- the one is set so the returns the mask 129 : 128 so that I get 8 binary bits as a string? See the bin() function in the sidebar in my topic for a function to display a number in its binary form. -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Struct the solution for Hex translation
Hi all, I read in some conversations that the struct module maybe helpful in converting Hex values to binary or decimal. Maybe I understood it incorrectly. Here is my problem. I have a 22 byte data packet on a tcp socket connection. My data field is from the 6th byte to byte 20. 14 bytes in total. The first two bytes of the data is a 16 bit value. Eg: \xe2\x01' I can the first byte into binary if I use 'e2', but I don't know how to get the '\x' out of the first byte to use it in python. My data has the '\x' and all I need is the 'e2'. Any advice of the usage of the struct module or how I can get rid of the '\x' in my data? Hopefully I have given enough information. Thanks Johan -- No virus found in this outgoing message. Checked by AVG Free Edition. Version: 7.5.441 / Virus Database: 268.18.2/692 - Release Date: 2007/02/18 04:35 PM ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Struct the solution for Hex translation
Johan Geldenhuys [EMAIL PROTECTED] wrote The first two bytes of the data is a 16 bit value. Eg: \xe2\x01' I can the first byte into binary if I use 'e2', but I don't know how to get the '\x' out of the first byte to use it in python. Are you sure it is there? Usually the \x is only part of the repr string, its not actually in the data. What do you get is you do: byte = data[0] # get the first byte print len(byte) # should only be one byte print byte # should get '\xe2' or whatever. My data has the '\x' and all I need is the 'e2'. If you do have the \xe2 that implies you have 4 characters, ie 4 bytes, so to get the real value use int(data[2:],16) Any advice of the usage of the struct module or how I can get rid of the '\x' in my data? I'm not sure where the struct module comes in? Are you using struct to read the data? If so you should be able to use unpack the data into the format you need by specifying a format string. eg struct.unpack('cc5s',data) Should return two characters(bytes) and a 98 character string. Like so: struct.unpack('cc5s','\x12\x23abcde') ('\x12', '#', 'abcde') Is that what you want? Notice that the first value is actially a single byte of value 12 hex. The \x are only in the display. The prompt is a great place to experiment with struct format strings etc. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Struct the solution for Hex translation
Here is what I have: data '\xa5\x16\x0b\x0b\x00\xd5\x01\x01\x01\x00\x00\xe3\x84(\x01\xc6\x00\x00\x17\x 01C\xc7' data[0] '\xa5' len(data[0]) 1 You see that data[0] is only one byte and it doesn't see all four characters. If I want to do this: int(data[0], 16) File console, line 1, in ? ''' exceptions.ValueError : invalid literal for int(): ¥ ''' But I can do this: int('a5', 16) 165 If I use data[0] as it is, I get errors. That why I want to know how I can strip away the '\x'. Here is some other code to convert Hex to Binary: hex2bin = { 0 : , 1 : 0001, 2 : 0010, 3 : 0011, 4 : 0100, 5 : 0101, 6 : 0110, 7 : 0111, 8 : 1000, 9 : 1001, a : 1010, b : 1011, c : 1100, d : 1101, e : 1110, f : } def hexBin(hexchars): ... s = for hexchar in hexchars: s += hex2bin[hexchar] return s.rstrip(\n) ... hexBin('a5') '10100101' This however does not work if my argument is '\xa5'. hexBin('\xa5') File console, line 1, in ? File console, line 5, in hexBin ''' exceptions.KeyError : '\xa5' ''' Thanks Johan -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Alan Gauld Sent: 19 February 2007 05:04 PM To: tutor@python.org Subject: Re: [Tutor] Struct the solution for Hex translation Johan Geldenhuys [EMAIL PROTECTED] wrote The first two bytes of the data is a 16 bit value. Eg: \xe2\x01' I can the first byte into binary if I use 'e2', but I don't know how to get the '\x' out of the first byte to use it in python. Are you sure it is there? Usually the \x is only part of the repr string, its not actually in the data. What do you get is you do: byte = data[0] # get the first byte print len(byte) # should only be one byte print byte # should get '\xe2' or whatever. My data has the '\x' and all I need is the 'e2'. If you do have the \xe2 that implies you have 4 characters, ie 4 bytes, so to get the real value use int(data[2:],16) Any advice of the usage of the struct module or how I can get rid of the '\x' in my data? I'm not sure where the struct module comes in? Are you using struct to read the data? If so you should be able to use unpack the data into the format you need by specifying a format string. eg struct.unpack('cc5s',data) Should return two characters(bytes) and a 98 character string. Like so: struct.unpack('cc5s','\x12\x23abcde') ('\x12', '#', 'abcde') Is that what you want? Notice that the first value is actially a single byte of value 12 hex. The \x are only in the display. The prompt is a great place to experiment with struct format strings etc. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -- No virus found in this incoming message. Checked by AVG Free Edition. Version: 7.5.441 / Virus Database: 268.18.2/692 - Release Date: 2007/02/18 04:35 PM -- No virus found in this outgoing message. Checked by AVG Free Edition. Version: 7.5.441 / Virus Database: 268.18.2/692 - Release Date: 2007/02/18 04:35 PM ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Struct the solution for Hex translation
You're way off base... :) On Feb 19, 2007, at 9:25 AM, Johan Geldenhuys wrote: Here is what I have: data '\xa5\x16\x0b\x0b\x00\xd5\x01\x01\x01\x00\x00\xe3\x84(\x01\xc6\x00 \x00\x17\x 01C\xc7' data[0] '\xa5' len(data[0]) 1 You see that data[0] is only one byte and it doesn't see all four characters. If I want to do this: int(data[0], 16) File console, line 1, in ? ''' exceptions.ValueError : invalid literal for int(): ¥ ''' But I can do this: int('a5', 16) 165 If I use data[0] as it is, I get errors. That why I want to know how I can strip away the '\x'. This is what you want to do: import struct struct.unpack('B',data[0]) (165,) Once again, the \x doesn't really exist, any more than the quotation marks do. They're just ways of indicating on the screen what kind of data is being displayed. Here is some other code to convert Hex to Binary: hex2bin = { 0 : , 1 : 0001, 2 : 0010, 3 : 0011, 4 : 0100, 5 : 0101, 6 : 0110, 7 : 0111, 8 : 1000, 9 : 1001, a : 1010, b : 1011, c : 1100, d : 1101, e : 1110, f : } def hexBin(hexchars): ... s = for hexchar in hexchars: s += hex2bin[hexchar] return s.rstrip(\n) ... hexBin('a5') '10100101' This however does not work if my argument is '\xa5'. hexBin('\xa5') File console, line 1, in ? File console, line 5, in hexBin ''' exceptions.KeyError : '\xa5' ''' This function is useless in this case because you don't actually have a string of letters and numbers; you just have raw binary data. -- -dave After all, it is not *that* inexpressible. -H.H. The Dalai Lama ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Struct the solution for Hex translation
Johan Geldenhuys [EMAIL PROTECTED] wrote data '\xa5\x16\x0b\x0b\x00\xd5\x01\x01\x01\x00\x00\xe3\x84(\x01\xc6\x00\x00\x17\x 01C\xc7' data[0] '\xa5' len(data[0]) 1 OK, So that tells you that you have one byte. The '\xa5' is a 4 character representation of that byte but it is only one byte with hex value a5. You don;t need to strip anything, you have your byte. int(data[0], 16) File console, line 1, in ? ''' exceptions.ValueError : invalid literal for int(): ¥ ''' Its already an int (well a byte) you don;t need to use int() int('a5', 16) 165 Because that passes the 2 byte string 'a5' to int. But you don't need that your byte actualoly is a5 If I use data[0] as it is, I get errors. That why I want to know how I can strip away the '\x'. The \x doesn't exist its purely a part of the replresentation that Python uses to display the data. Its like the L at the end of a long interer. The number doesn't really have an L at the end its just put there by Python to show the type. Similarly the '\x' is prepended by Python to show that this is a hex value. def hexBin(hexchars): ... s = for hexchar in hexchars: s += hex2bin[hexchar] return s.rstrip(\n) ... hexBin('a5') '10100101' This however does not work if my argument is '\xa5'. hexBin('\xa5') File console, line 1, in ? File console, line 5, in hexBin ''' exceptions.KeyError : '\xa5' ''' Because you are now passing a single character which is not a valid hex character. So far as I can see you actually have the data you want! -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Struct the solution for Hex translation
Thanks, Dave. On the struct module, How can I het the binary 1's and 0's of the Hex value? Let say I want to get the 8 bit value of '\xe2', can I use struct to convert that into binary code so that I get 8 binary bits as a string? Thanks for helping with struct. Johan -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of David Perlman Sent: 19 February 2007 05:56 PM To: tutor@python.org Subject: Re: [Tutor] Struct the solution for Hex translation You're way off base... :) On Feb 19, 2007, at 9:25 AM, Johan Geldenhuys wrote: Here is what I have: data '\xa5\x16\x0b\x0b\x00\xd5\x01\x01\x01\x00\x00\xe3\x84(\x01\xc6\x00 \x00\x17\x 01C\xc7' data[0] '\xa5' len(data[0]) 1 You see that data[0] is only one byte and it doesn't see all four characters. If I want to do this: int(data[0], 16) File console, line 1, in ? ''' exceptions.ValueError : invalid literal for int(): ¥ ''' But I can do this: int('a5', 16) 165 If I use data[0] as it is, I get errors. That why I want to know how I can strip away the '\x'. This is what you want to do: import struct struct.unpack('B',data[0]) (165,) Once again, the \x doesn't really exist, any more than the quotation marks do. They're just ways of indicating on the screen what kind of data is being displayed. Here is some other code to convert Hex to Binary: hex2bin = { 0 : , 1 : 0001, 2 : 0010, 3 : 0011, 4 : 0100, 5 : 0101, 6 : 0110, 7 : 0111, 8 : 1000, 9 : 1001, a : 1010, b : 1011, c : 1100, d : 1101, e : 1110, f : } def hexBin(hexchars): ... s = for hexchar in hexchars: s += hex2bin[hexchar] return s.rstrip(\n) ... hexBin('a5') '10100101' This however does not work if my argument is '\xa5'. hexBin('\xa5') File console, line 1, in ? File console, line 5, in hexBin ''' exceptions.KeyError : '\xa5' ''' This function is useless in this case because you don't actually have a string of letters and numbers; you just have raw binary data. -- -dave After all, it is not *that* inexpressible. -H.H. The Dalai Lama ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -- No virus found in this incoming message. Checked by AVG Free Edition. Version: 7.5.441 / Virus Database: 268.18.2/692 - Release Date: 2007/02/18 04:35 PM -- No virus found in this outgoing message. Checked by AVG Free Edition. Version: 7.5.441 / Virus Database: 268.18.2/692 - Release Date: 2007/02/18 04:35 PM ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor