Python Forum
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Byte-Array
#1
Hello,

I would like to assign hex values to a byte array, however for some values python assign letters to the array instead hex values.

For example:
length = bytes(3)
barray = bytearray(length)

# construct example array
barray[0] = 0xd0
barray[1] = 0x13
barray[2] = 0x74

print(barray) 
It outputs:
bytearray(b'\xd0\x13t')
But I would like to have:
bytearray(b'\xd0\x13\x74')
Where is the problem?

Best regards,
Olli
Reply
#2
No problem - HEX values that correspond to ASCII characters are shown like those characters
Output:
In [7]: chr(0x74) Out[7]: 't'

I would have used array to build bytestring - initializing values one-by-one does not look very Pythonic
Output:
In [12]: import array In [13]: array.array('B', (0xd0, 0x13, 0x74)).tobytes() Out[13]: b'\xd0\x13t'
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#3
Try the following construction:
for x in barray:
    print(hex(x)) 
Lewis
To paraphrase: 'Throw out your dead' code. https://www.youtube.com/watch?v=grbSQ6O6kbs Forward to 1:00
Reply
#4
I guess there is no one right way how you can solve this task.

You can work with binascii.hexlify
from binascii import hexlify


ba = bytearray(b'\xd0\x13t')
hexstr = hexlify(ba).decode() # hexlify returns bytes
print(hexstr)
Another method could be str-formatting:
ba = bytearray(b'\xd0\x13t')
hexstr = ''.join(format(integer, 'x') for integer in ba) 
print(hexstr)
For Python 3.6 fans with format string interpolation:
ba = bytearray(b'\xd0\x13t')
hexstr = ''.join(f'{integer:x}' for integer in ba) 
print(hexstr)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  extract only text strip byte array Pir8Radio 7 2,790 Nov-29-2022, 10:24 PM
Last Post: Pir8Radio
  'utf-8' codec can't decode byte 0xe2 in position 122031: invalid continuation byte tienttt 12 11,354 Sep-18-2020, 10:10 PM
Last Post: tienttt
  convert array of numbers to byte array adetheheat 3 2,731 Aug-13-2020, 05:09 PM
Last Post: bowlofred
  Convert even byte array to odd medatib531 1 2,174 Mar-17-2020, 02:48 AM
Last Post: scidam
  'utf-8' codec can't decode byte 0xda in position 184: invalid continuation byte karkas 8 31,475 Feb-08-2020, 06:58 PM
Last Post: karkas
  Byte array is sorted when sending via USB daviddlc68 1 2,779 Aug-16-2019, 10:11 AM
Last Post: wavic
  Reading data from serial port as byte array vlad93 1 11,981 May-18-2019, 05:26 AM
Last Post: heiner55
  Read Byte Array from Zigbee coordinator using python jenkins43 3 3,815 Mar-26-2019, 03:03 PM
Last Post: micseydel
  Not able to Read byte Array From Wireless Coordinator jenkins43 0 1,859 Mar-18-2019, 07:05 AM
Last Post: jenkins43
  4 byte hex byte swap from binary file medievil 7 21,935 May-08-2018, 08:16 AM
Last Post: killerrex

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020