-2

I am trying to convert this RFID Tag number got from this code;

import serial

ser = serial.Serial()
ser.port = "COM1"
ser.baudrate = 9600
ser.timeout = 3
ser.open()

if ser.open is True:
    print "Port Not open"

while ser.isOpen():
    #ser.timeout = 7

    response = ser.read(17)
    response = response.encode('hex')

    print response

I am getting this 0000000000000000000213780510015dff which is a hexadecimal number, but I want to convert it to decimal or string. When I try to do that, I am getting a token error. How can I fix that?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343

2 Answers2

5

You say you want to "convert… to string".

You can use unhexlify to do that, or decode('hex').

However, in your case, the only reason you have hex in the first place is that you called encode('hex'), so just… don't do that.

If you want to decode it to an int or a Decimal or something, you can do that by using the appropriate constructor, as Maxime's answer shows. However, rather than converting to hex just to decode as an int, you might want to just decode it directly. Or maybe you want to decode the hex string into a decimal string? Or maybe this is some UUID-style structure, and you want to use struct.unpack to decode it into pieces? Or…? Without knowing exactly what you're trying to do, it's hard to give an exact answer…

abarnert
  • 354,177
  • 51
  • 601
  • 671
  • I laughed a bit... I see this way too often. Very good point :P – Ricky Mutschlechner Sep 23 '13 at 20:29
  • Well, Before I converted to hexadecimal, I had something like this ·☺☺ ☻‼x♣►☺]  ☻‼x♣►☺]  so if that can be directly converted to decimal It sould even be better. – user2785468 Sep 23 '13 at 21:30
  • @user2785468: Yes, that's a string, and you asked to "convert it to decimal or string". If that's not what you want, you will need to tell us what you want. I have no idea what you mean by "converted to decimal", or what value you're hoping to get. If you want to treat the 17 bytes as a 68-bit integer in big-endian order, Maxime's answer tells you how to do that. If you want to traat them as a 68-bit integer in little-endian order, someone can show you how to do that. But if we have to guess every possible thing you might want to do and show all of them, that's not going to happen. – abarnert Sep 23 '13 at 21:51
  • Well, what I wanted was that converted to a string since to use int(str, 16) needs a string or just a direct way to getting an integer out of it. anyway maxime tells me what I have (x = 0000000000000000000213780510015dff) is a piece of code not a string so let me see if I can give it a different approach instead of having to convert it to hex. Anyway thanks so much for your help. – user2785468 Sep 23 '13 at 22:06
  • @user2785468: What is the "that" converted to a string? And converted in what way? Do you really want to treat the bytes as a 68-bit big-endian integer, or are you just trying random things in hopes that you can get something to run, whether it gives you the right answer or not? – abarnert Sep 23 '13 at 22:22
  • that refers to the hexadecimal number(0000000000000000000213780510015dff) which am getting after reading the rfid tag. This number is what I want to convert to inerger so that I can add it to my database as an interger – user2785468 Sep 23 '13 at 22:30
  • What you get from reading the `rfid` tag is a 17-character string of bytes. Calling `encode('hex')` on it gives you a 34-character hex string with each pair of characters representing one of those bytes. Calling `int(response, 16)` will get you the 68-bit integer represented by that same hex string in big-endian order. If that's what you want, Maxime's answer is all you need. But I'm not sure why you think that's what you want. Is that really what the RFID tag value is supposed to be, a 68-bit integer? That's a very odd data type. – abarnert Sep 23 '13 at 22:46
  • I had tried that but it failed, let me try writing different data to the tag instead or change the type of data in that column of my database. Thanks so much for your help. – user2785468 Sep 23 '13 at 22:53
  • Just saying "I had tried that but it failed" doesn't help anyone debug your code. You have to show us the code you tried, and the error (or incorrect results) that you got (by editing your question, or posting something at pastebin, or whatever). – abarnert Sep 23 '13 at 23:00
3

You can use int to convert a hexadecimal number into an integer.

>>> int("0000000000000000000213780510015dff", 16)
149595175772052991
Maxime Chéramy
  • 17,761
  • 8
  • 54
  • 75
  • the value am getting is not a string, it is like this, x = 0000000000000000000213780510015dff, so if you try instantiate this, it gives you x = 0000000000000000000213780510015dff ^ SyntaxError: invalid token – user2785468 Sep 23 '13 at 21:22
  • @user2785468: `x = 0000000000000000000213780510015dff` is a line of code, not a value. And an invalid line of code at that, hence the `SyntaxError`. (Or, if it is a value, like the string `"x = 0000000000000000000213780510015dff", then it's a string.) – abarnert Sep 23 '13 at 21:52
  • That's what I had thought at first but I was not sure but since now I am, Thanks to you, I will have to give it another approach. Thanks for your assistance so much. – user2785468 Sep 23 '13 at 22:00