1

Python Version: 3.7.2
I need to convert a string in ASCII like Øâþ  ÿþ !Zk2ìm "Ï"À>q úÞ to Hexademical, which in this case would be d8 e2 02 12 02 fe 01 20 9b 10 20 20 03 ff 07 fe 20 20 21 5a 6b 32 ec 17 6d 20 0e 22 cf 22 c0 3e 71 20 02 20 03 fa de. I found several solutions for doing this on Python 2, however I can't find any way of doing this on Python 3.

To summarise: The intender behaviour is ASCII to HEX as follows:
Øâþ  ÿþ !Zk2ìm "Ï"À>q úÞ TO d8 e2 02 12 02 fe 01 20 9b 10 20 20 03 ff 07 fe 20 20 21 5a 6b 32 ec 17 6d 20 0e 22 cf 22 c0 3e 71 20 02 20 03 fa de.

I've even checked on https://www.rapidtables.com/convert/number/ascii-to-hex.html and found it works, but I'm unable to implement it in Python 3.

1

2 Answers 2

2

You may use the code:

print(*[hex(ord(letter))[2:] for letter in 'Øâþ   ÿþ  !Zk2ìm "Ï"À>q  úÞ'])

which gives the following output:

d8 e2 fe 20 10 20 20 ff fe 20 20 21 5a 6b 32 ec 6d 20 e 22 cf 22 c0 3e 71 20 20 fa de

ord() - get ascii code, hex() - get hex from int, [2:] - to omit 0x in every number.

EDIT

Slightly modified version (to get 0e instead of e):

string = 'Øâþ   ÿþ  !Zk2ìm "Ï"À>q  úÞ'
print(*['{:02x}'.format(ord(letter)) for letter in string])
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks Poolka, but in 6d 20 e 22 cf subsegment, you got a lone e character in 1 byte, that's the problem that I was facing when I tried to omit the starting 2 characters from each byte. e is a single hex character which should take half a byte / 4 bits instead of the 8 bits it's taking currently
@HarshitJindal e is 0e I guess. The leading zero is omitted by hex() function. Moreover e is pretty legal hex number. You may add leading zero if you need.
thank you so much. I can't tell how much I tried looking it up, but only found anwers for Python 2 or answers where the output had non-hex characters as well like > or ! as in @Error - Syntactical Remorse's answer too. Could you explain why that solution led to non-hex characters in a hex output?
@HarshitJindal The answer by @Error - Syntactical Remorse is very similar. The difference is another representation - bytearray instead of plain string.
@HarshitJindal Added slighltly modified version. Followed advice by Error - Syntactical Remorse. Seems to work just fine.
|
0

Use ord():

s = 'Øâþ   ÿþ  !Zk2ìm "Ï"À>q  úÞ'
bytes = bytearray(ord(char) for char in s)
print(bytes)

Output:

bytearray(b'\xd8\xe2\xfe \x10  \xff\xfe  !Zk2\xecm \x0e"\xcf"\xc0>q  \xfa\xde')

That being said I can't match your output exactly because you copied and pasted a garbage char:

print(''.join(chr(char) for char in bytes)) # Øâþ   ÿþ  !Zk2ìm "Ï"À>q  úÞ

1 Comment

I was able to get this output but isn't this form called hex-ascii? I want my output to be plain hex, without any ascii characters in the data. Checkout the website link I added in the question. I want that type of output

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.