close

[Solved] ValueError: invalid literal for int() with base 16: ‘\x0e\xa3’ Python

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error ValueError: invalid literal for int() with base 16: ‘\x0e\xa3’ Python in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How ValueError: invalid literal for int() with base 16: ‘\x0e\xa3’ Python Error Occurs?

Today I get the following error ValueError: invalid literal for int() with base 16: ‘\x0e\xa3’ Python in python.

How To Solve ValueError: invalid literal for int() with base 16: ‘\x0e\xa3’ Python Error ?

  1. How To Solve ValueError: invalid literal for int() with base 16: '\x0e\xa3' Python Error ?

    To Solve ValueError: invalid literal for int() with base 16: '\x0e\xa3' Python Error Because int is not really for working with binary data, but with hexadecimal strings like: EF1D.

  2. ValueError: invalid literal for int() with base 16: '\x0e\xa3' Python

    To Solve ValueError: invalid literal for int() with base 16: '\x0e\xa3' Python Error Because int is not really for working with binary data, but with hexadecimal strings like: EF1D.

Solution 1

I think you should use struct module and unpack your binary data like this:

struct.unpack("h", x)

Because int is not really for working with binary data, but with hexadecimal strings like: EF1D.

When you did x=ser.read(2) you received two bytes of binary data, there are two types of number representation supported by struct library: short(h) and unsigned short(H). Function struct.unpack receives two argument:

  • structure specification (a string of format characters)
  • binary data

and returns a tuple with unpacked values(only one int in your case).

So you need to change string w=int(x, 16) to w = struct.unpack("h", x)[0] or to w = struct.unpack("H", x)[0], it depends on data type.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read