Manually create a hex value - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Manually create a hex value (/thread-24594.html) |
Manually create a hex value - Stas43 - Feb-21-2020 Help collect a string for correct conversion: It works correctly: a = '\xcf' t = a.encode("latin-1").decode("cp1251") print (t):~/Python/5$ ./test1.py П And so not right. b = r'\x' + 'cf' t = b.encode("latin-1").decode("cp1251") print (t):~/Python/5$ ./test1.py \xcf How is it right to do that? RE: Manually create a hex value - Larz60+ - Feb-21-2020 the \x is an escape character which says take the following as hexadecimal number once separated from each other, the \x by itself fails because there is no value immediately following a = '' + '\xcf' would work. RE: Manually create a hex value - Stas43 - Feb-25-2020 (Feb-21-2020, 12:18 PM)Larz60+ Wrote: the \x is an escape character which says take the following as hexadecimal number Of course, your option works. And how does it differ from my version in the first post ... a = '\xcf' ...? I want to collect! variable b from two different lines: '\x' and 'cf' but Python doesn't understand this! RE: Manually create a hex value - Stas43 - Feb-25-2020 Here is the right solution, in my case: #!/usr/bin/python3 #coding: utf-8 import binascii a = 'cf' print (binascii.unhexlify(a)) print (binascii.unhexlify(a).decode("cp1251")):~/Python/5$ ./test1.py b'\xcf' П RE: Manually create a hex value - DeaD_EyE - Feb-25-2020 Try: print (binascii.unhexlify(a).decode())Then it's encoded by default as utf-8. You'll get a different result. RE: Manually create a hex value - Stas43 - Feb-27-2020 (Feb-25-2020, 08:15 AM)DeaD_EyE Wrote: Try:print (binascii.unhexlify(a).decode())Then it's encoded by default as utf-8. Yes. In my case, this is enough |