Home/Support/Support Forum/IEEE 754 conversion in Python and reading individual bytes
Welcome to Digi Forum, where you can ask questions and receive answers from other members of the community.

IEEE 754 conversion in Python and reading individual bytes

0 votes
Hi,
1. If i wanted to just read a particular number of bytes, for example, byte 4-8 in a 15 byte data stream, how would i do that in python please?

2. Does anybody know how to convert a 32 bit hex data into IEEE 754 floating point in python please!
asked Oct 12, 2009 in Python by meme New to the Community (42 points)

Please log in or register to answer this question.

6 Answers

0 votes
> 1. If i wanted to just read a particular number of
> bytes, for example, byte 4-8 in a 15 byte data
> stream, how would i do that in python please?

Once you've read your 15 bytes into a string, you can use slices to extract the sub-string that you desire.

For example:
a = 'abcdefghijklmnp'
b = a[4:9]

will extract the bytes with subscripts 4 through 8 inclusive.

>
> 2. Does anybody know how to convert a 32 bit hex data
> into IEEE 754 floating point in python please!

IEEE 754 has more than one 32 bit representation for data. However, I'll assume you mean something like a single precision float. If that's the case check out.

http://en.wikipedia.org/wiki/Binary32

to make sure. Probably the simplest way is to use the struct module to unpack it as a float. If you have your 32 bits as a binary string in variable h, you would just do:

struct.unpack('f', h)

and it would return the data as a float object.

However,you mention that it is "hex" data. So if it's a formatted string represented in hex, you'll need to get the data into a packed binary representation first. One way would be to use the built in int function int(h, 16) for example, and then unpack that.
answered Oct 12, 2009 by DigiGuy42 Community Contributor (102 points)
0 votes
Remember that unlike C/C++, where a "ASCII string" is a null-terminated thing of variable length and a "binary string" is a byte array of fixed length, in Python both are just "strings" which can be queried for length or segments as Digiguy says above.

So if you receive 15-bytes from a serial port or UDP/ZB socket, it is a "string" regardless of being text or binary. I mention this because some Python beginners get this mind-set that binary "byte arrays" are somehow different than strings. But st = '\x00\x01\x02\x03' creates a 4-byte string just as does st = 'ABCD'.

(Side-Note: yes, some C/C++/API into Python pass in a raw non-Python "byte buffers", but those are not common outside of things like raw Windows MFC/API linkages).
answered Oct 12, 2009 by lynnl Veteran of the Digi Community (1,047 points)
0 votes
Thank you to both of you! That was very helpful!
answered Oct 13, 2009 by meme New to the Community (42 points)
0 votes
hi, i was wondering if you can help me again please,
i'm reading raw hex data and i want to be able to extract particular bytes and display its decimal equivalent.
For example, if the raw data is: aa1084e89d41000000001e ...
i want to extract out the following 4 bytes: 84e89d41
and then i want to read it as 419de884 (so LSB is 8 and MSB is 1 i presume, and each byte/pair is reversed). How can i do this please?

Sorry for such basic questions .. i'm no good with programming.
answered Oct 15, 2009 by meme New to the Community (42 points)
0 votes
hi, i was wondering if you can help me again please,
i'm reading raw hex data and i want to be able to extract particular bytes and display its decimal equivalent.
For example, if the raw data is: aa1084e89d41000000001e ...
i want to extract out the following 4 bytes: 84e89d41
and then i want to read it as 419de884 (so LSB is 8 and MSB is 1 i presume, and each byte/pair is reversed). How can i do this please?

Sorry for such basic questions .. i'm no good with programming.
answered Oct 15, 2009 by meme New to the Community (42 points)
0 votes
I first assume that your representation is actually raw octets and *not* the letter "A" followed by the letter "A" followed by "1", etc.

Look at the "struct" module.

Here is an example:

data = "\xAA\x10\x84\xE8\x9D\x41\x00\x00\x00\x00\x1E"
print len(data)

# One way to get the value from unpack
values = struct.unpack('>L', data[2:6])
value = values[0]
print "%08x -- %d" % (value, value)

# Another way to get the value
value, = struct.unpack('<L', data[2:6])
print "%08x -- %d" % (value, value)
answered Oct 15, 2009 by 52637p Community Contributor (59 points)
...