Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Convert string of integers to Ascii

Currently I work on a project with an NFC tag reader. All data is stored in decimal values and I managed to track the data I need down and cut every special character out of it. I now want to convert it to ascii so it prints out a readable version of the data.

The variable setvar is a string and has the value "84 97 103 32 78 117 109 98 101 114 32 49"

This is the code I try to use:

print "ASCII value: ", ´,´.join(str(chr(c)) for c in setvar)

Sadly this produces an error that this function needs integers. Now my next step would be to convert my string to an array(?) and read each item of that array with a loop. Sadly I have no idea how to do that.

How to do that?

like image 559
Hyxeln Avatar asked Oct 28 '25 17:10

Hyxeln


2 Answers

I'd just decode the corresponding bytes.

>>> s = "84 97 103 32 78 117 109 98 101 114 32 49"
>>> bytes(map(int, s.split())).decode()
'Tag Number 1'

(Note that decode can take an optional encoding argument, the default value is 'utf-8'.)

like image 50
timgeb Avatar answered Oct 31 '25 06:10

timgeb


You can try this:

print ', '.join(chr(int(i)) for i in setvar.split())

chr will convert any integer to its alphabetical ASCII letter.

like image 26
Ajax1234 Avatar answered Oct 31 '25 08:10

Ajax1234



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!