Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Convert unicode to decimal

I have a string like this which i want to convert to only decimal data:

"\u0006;\u0003\u001588D402"

I’m not sure what the output should look like, but I imagine it should be like this:

"631588D402"

I have tried to separate each hexadecimal data and then join them but have been unable to either separate them or convert them correctly. Any idea?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

you can go through the string and if the current char is a hex number you add it to new_s elif char in string.printable you skip it (because it is not a hex number and it is printable so it’s not some value) else you convert char to his value and then to hex and add the hex value to new_s

import string


s = "\u0006;\u0003\u001588D402"
new_s = ""
for char in s:
    if char in "0123456789ABCDEF":  # if char is a hex number
        new_s += char
    # the char isn't a hex number and is printable (removes ;)
    elif char in string.printable:
        pass
    else:
        new_s += hex(ord(char))[2:]
# in one line:
# new_s = "".join([char if char in "0123456789ABCDEF" else "" if char in string.printable else hex(ord(char))[2:] for char in s])
print(new_s)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading