hex array to array of 16 bit numbers?

T

Todd Gardner

Hello everyone,

I would really appreciate some of your expertise or even just
pointers. How do I convert this hex array variable "buffer" to array
of 16 bit numbers?

repr(buffer.raw)= '\xcd\x00\xce\x00\xce\x00\xce\x00'
Any ideas would be greatly appreciated!

Todd
 
B

Bjorn Pettersen

[posted and mailed]

(e-mail address removed) (Todd Gardner) wrote in

Hello everyone,

I would really appreciate some of your expertise or even just
pointers. How do I convert this hex array variable "buffer" to array
of 16 bit numbers?


repr(buffer.raw)= '\xcd\x00\xce\x00\xce\x00\xce\x00'

Any ideas would be greatly appreciated!

Todd

First I think your missing the fact that the 'hex' values you're seing is
simply an encoding of the byte value at that byte-index in the buffer, ie.
your buffer starts with bytes containing the following values:

[205] [0] [206] ...

besides that, you're looking for the struct module :)

-- bjorn
 
T

Todd Gardner

That's all I needed to make it work.

Thank you for pointing me to the correct module.

Todd
 
R

Robin Munn

Todd Gardner said:
Hello everyone,

I would really appreciate some of your expertise or even just
pointers. How do I convert this hex array variable "buffer" to array
of 16 bit numbers?


repr(buffer.raw)= '\xcd\x00\xce\x00\xce\x00\xce\x00'

Any ideas would be greatly appreciated!

I think the struct.unpack() method is what you're looking for here:

http://www.python.org/doc/current/lib/module-struct.html

If you've got four 16-bit numbers, then:

import struct
status_tuple = struct.unpack('4h', buffer.raw)

should give you what you want. Read the documentation for the meaning of
the various format characters.
 
F

Francis Avila

Todd Gardner wrote in message
That's all I needed to make it work.

Thank you for pointing me to the correct module.

Todd

Just as a heads up, your code uses the name "buffer", which clobbers the
builtin function "buffer." (Which, honestly, I've never seen used, but
still....)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top