-1

I'm trying to unapck a list from hex to integers in python.

So for example:

hexValues = '\x90\x82|uj\x82ix'
decodedHex = struct.unpack_from('B', hexValues,0)
print decodedHex

Which would print (144,) and nothing else. Is there any way I can loop through this string to get all values? (bear in mind the length of hex values is much longer than the example given.)

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
cryptiblinux
  • 29
  • 3
  • 12

1 Answers1

1

You can get all the values at once:

import struct

hexValues = '\x90\x82|uj\x82ix'
format = '%dB' % len(hexValues)
decodedHex = struct.unpack_from(format, hexValues)
print(decodedHex)  # -> (144, 130, 124, 117, 106, 130, 105, 120)

As Jon Clements helpfully pointed out in a comment, you don't really need to use the struct module:

decodedHex = tuple(bytearray(hexValues))
print(decodedHex)  # -> (144, 130, 124, 117, 106, 130, 105, 120)
martineau
  • 119,623
  • 25
  • 170
  • 301
  • Possible alternative `list(bytearray(hexValues))` – Jon Clements Apr 20 '16 at 17:59
  • This is great thanks! Any idea why so many values are printed? I thought \x is an escape character indicating the next two characters are hexadecimal values. Only 3 integers should be returned? – cryptiblinux Apr 20 '16 at 18:01
  • @javasaucebiner you'll get as many as `len(hexValues)` - what were you expecting? – Jon Clements Apr 20 '16 at 18:01
  • @javasaucebiner: there are 8 characters, so 8 byte values. – Martijn Pieters Apr 20 '16 at 18:01
  • @javasaucebiner: `|`, `u`, `j`, etc. are all printable ASCII values, so are not displayed with `\xhh` escape sequences. They are not part of the `\xhh` sequences, which are always 2 hex digits long. – Martijn Pieters Apr 20 '16 at 18:02
  • javasaucebiner: Because `struct.unpack()` always returns a tuple of values, even in the code in your question it's returning one , but it only contains a single value — `(144,)` — because your format string specifies only one byte. – martineau Apr 20 '16 at 18:03
  • @JonClements I am trying to get IQ data. I don't know if any of you know about this. But, do you think 144 = I value, 130 = Q value 124 = I Value etc? – cryptiblinux Apr 20 '16 at 18:06
  • @javasaucebiner if you're aware of the criteria - surely you can work with the values this answer provides and that's your next challenge? :p – Jon Clements Apr 20 '16 at 18:14
  • What criteria? sorry? I was just hoping for confirmarion – cryptiblinux Apr 20 '16 at 18:20
  • @martineau I don't follow. I'm trying to find the IQ values to plot a graph. I will perform an operation on these values to return a value between -1 and +1. – cryptiblinux Apr 20 '16 at 20:42
  • Hi @martineau, could you please explain the line '%dB' % len(hexValues) – cryptiblinux May 09 '16 at 20:07
  • javasaucebiner: It creates a string with the decimal number of hex values in the `hexValues` string followed by a the character `B` which is the [format character](https://docs.python.org/2/library/struct.html#format-characters) for the **unsigned char** type. If you read the documentation under the table of format characters it says that they may be preceded by an integral repeat count, so for example, a format string of `"2B"` means unpack 2 values interpreting each as an unsigned 8-bit value between 0-255. For that case, it does the same thing as the literal `"BB"` would. – martineau May 09 '16 at 20:50