Hello guest, if you read this it means you are not registered. Click here to register in a few simple steps, you will enjoy all features of our Forum.
Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 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
Quote
#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 your favorite Python shell
  • 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.
Quote
#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
Quote
#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)
My code examples are always for Python 3.x.
All humans together. We don't need politicians!
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  4 byte hex byte swap from binary file medievil 7 179 May-08-2018, 08:16 AM
Last Post: killerrex
  python charmap codec can't decode byte X in position Y character maps to < undefined> owais 9 7,107 Apr-28-2018, 10:52 PM
Last Post: abadawi
  I have an array, how can I search a seperate file for the contents of my array? Mr_Keystrokes 0 120 Mar-13-2018, 02:25 PM
Last Post: Mr_Keystrokes
  get the content of the byte as string ricardons 5 191 Mar-02-2018, 02:41 PM
Last Post: ricardons
  converting array of things to byte stream to send bigjoey 2 141 Feb-27-2018, 12:56 PM
Last Post: bigjoey
  byte string Skaperen 5 301 Feb-04-2018, 08:58 AM
Last Post: Gribouillis
  byte string in python2 Skaperen 4 433 Nov-23-2017, 03:13 AM
Last Post: Skaperen
  Convert list of integers to bit-mapped byte eccles 10 812 Oct-25-2017, 07:35 PM
Last Post: eccles
  typeerror:byte like object is required rajeev1729 1 515 Sep-15-2017, 06:23 PM
Last Post: nilamo
  error in script "Expected 2D array, got 1D array instead:" drogontargaryen 0 5,374 Aug-07-2017, 04:32 PM
Last Post: drogontargaryen

Forum Jump:


Users browsing this thread: 1 Guest(s)