0

I am new Python user and I am trying to convert hexadecimal string to decimal integer with Python. In my system, I send analogue values like :

import serial 
import binascii 

ser = serial.Serial(port = 'COM3',
    baudrate = 9600,
    bytesize=8,
    parity='N',
    stopbits=1,
    timeout =None) # my parameter to communicate with my card
ser.open() 
a = binascii.a2b_hex('010f0001000000c8d9') # transform hexadecimal under binary format (no problem)
u = ser.write(a) # send binary data (no problem, it works fine)
i = ser.read(u) # read the answer 
i 
# I get that hexadecimal string : '\x02\x01d\x0f\x00\x00\x02\xbf7' 

What I tried :

int(i, 0)

it did not work,and I did not find anywhere how I can translate that into a decimal. Does someone know?

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
Dianou
  • 9
  • 2

1 Answers1

0

You are getting the binary representation in i. To keep with the logic of your program you can use binascii.b2a_hex to convert it to a string of hex digits, and then use int(string,16) to convert it to a decimal int.

b = binascii.b2a_hex(i)
number = int(b,16)

Mind you, it's going to be a long int, so handle it with care

Noel Segura Meraz
  • 2,265
  • 1
  • 12
  • 17