Bottom Page

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
Quote
#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

Quote
#3
Ok Thanks, will check
Quote
#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.
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Bytearray substitution Vismuto 1 107 Apr-14-2020, 09:18 AM
Last Post: TomToad
  int() function with bytearray() Jona66 1 290 Sep-08-2019, 12:41 PM
Last Post: ichabod801
  Conversion needed from bytearray to Floating point braveYug 1 799 May-07-2018, 12:23 PM
Last Post: snippsat
  Windows DIB format in bytearray to image? dusca 2 829 Mar-28-2018, 10:35 PM
Last Post: dusca
  Bytearray questions mattps 2 11,938 Mar-25-2018, 06:54 AM
Last Post: mattps
  ByteArray outside while true Schampbakken 5 1,554 Feb-18-2018, 02:02 PM
Last Post: buran
  Error when converting MATLAB's datenum to Python's datetime zoya2385 5 3,937 Apr-03-2017, 06:01 PM
Last Post: zivoni
  readinto a bytearray slice bhowerter 3 3,561 Sep-23-2016, 10:04 PM
Last Post: micseydel

Forum Jump:


Users browsing this thread: 1 Guest(s)