Python Forum
Value error when converting hex value to bytearray
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Value error when converting hex value to bytearray
#1
Hi,
I am getting ValueError: non-hexadecimal number found in fromhex() arg at position 7.
Below is the code :
Cmd1 = bytearray.fromhex("08 22 1 0 1 0 D4");

Please let me know how to solve this issue
Reply
#2
It's expecting (hexpecting?) two-digit hex numbers.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Ok Thanks, will check
Reply
#4
input_hex = '08 22 1 0 1 0 D4'

# you can strip the white spaces, but the number of hex values have to be even.
data = bytearray.fromhex(input_hex.replace(' ', ''))
print(data)
Output:
bytearray(b'\x08"\x10\x10\xd4')
If you are using groups, you have to be consistent. Always two hex values have to be grouped together, if white space is used.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
i get the same result and wonder where the "22" ended up. i take out the "22" and still get that same result.
Output:
>>> input_hex = '08 1 0 1 0 D4' >>> data = bytearray.fromhex(input_hex.replace(' ', '')) >>> print(data) bytearray(b'\x08\x10\x10\xd4') >>>
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
You are responding ro a 4 year old thread. But to answer your question, 0x22 == ord('"'). If you look closely you wil see the 22 is not left out, it is printed as a double quote.
Reply
#7
broken_input_hex = "08 1 0 1 0 D4"
data = bytes(int(hex_val, 16) for hex_val in broken_input_hex.split())
I think, the name of the broken_input_hex gives a hit, what's wrong.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#8
is responding to an old thread that is still opened a bad thing? i can understand not noticing a " in the result being a bad thing. i'll blame the font my xterm uses for that. the " was actually hard to see. but i was not looking at it for 4 years. i think threads should be auto-closed after 6 months of no activity. but, that's just an opinion, not worth the bytes that encode it.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  appending to a bytearray Skaperen 21 13,319 Mar-19-2023, 11:05 PM
Last Post: Skaperen
  Split Bytearray into separate Files by Hex delimter lastyle 5 2,481 Mar-09-2023, 07:49 AM
Last Post: bowlofred
  Save multiple Parts of Bytearray to File ? lastyle 1 907 Dec-10-2022, 08:09 AM
Last Post: Gribouillis
  bytearray object - why converting to ascii? Chepilo 2 1,529 Nov-21-2022, 07:25 PM
Last Post: Chepilo
  Bytearray substitution Vismuto 1 2,561 Apr-14-2020, 09:18 AM
Last Post: TomToad
  int() function with bytearray() Jona66 1 2,362 Sep-08-2019, 12:41 PM
Last Post: ichabod801
  Conversion needed from bytearray to Floating point braveYug 1 4,062 May-07-2018, 12:23 PM
Last Post: snippsat
  Windows DIB format in bytearray to image? dusca 2 2,717 Mar-28-2018, 10:35 PM
Last Post: dusca
  Bytearray questions mattps 2 14,335 Mar-25-2018, 06:54 AM
Last Post: mattps
  ByteArray outside while true Schampbakken 5 4,322 Feb-18-2018, 02:02 PM
Last Post: buran

Forum Jump:

User Panel Messages

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