0

Trying to convert MAC into bytearray format.

Tried to convert into utf-8 but didn't get the output as expected.

values=("002A2C2D123A")
update_mac1=bytearray.fromhex(values)

It does not like byte number 5 which is 12 this is what my output looks like

bytearray(b'\x00*,-\x12:')

i'm looking for something like:

(b'\x00\x2A\x2C\x2D\x12\x3A
Alexandre B.
  • 5,387
  • 2
  • 17
  • 40
sameer
  • 9
  • 2
  • Have a look at this post - https://stackoverflow.com/questions/5649407/hexadecimal-string-to-byte-array-in-python – dreamzboy Aug 01 '19 at 22:39

1 Answers1

0

That's a matter of string formatting. You got the value you expected, but it doesn't print how you expected it to.

>>> print(b'\x00\x2A\x2C\x2D\x12\x3A')
b'\x00*,-\x12:'

If you want to print the string you mentioned, you can format each character in the sequence.

>>> print(''.join(f'\\x{c:02x}' for c in b'\x00\x2A\x2C\x2D\x12\x3A'))
\x00\x2a\x2c\x2d\x12\x3a

(That is a Python 3.6 version of this answer.)

But be aware that the string printed out actually includes \x, and isn't a byte array.

jirassimok
  • 3,850
  • 2
  • 14
  • 23
  • What exactly are you trying to do? Your code correctly converts any hexadecimal string into a `bytearray`, but it sounds like you might be looking for something else. – jirassimok Aug 05 '19 at 15:08