Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Manually create a hex value
#1
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?
Reply
#2
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.
Reply
#3
(Feb-21-2020, 12:18 PM)Larz60+ Wrote: 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.

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!
Reply
#4
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'
П
Reply
#5
Try:
print (binascii.unhexlify(a).decode())
Then it's encoded by default as utf-8.
You'll get a different result.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#6
(Feb-25-2020, 08:15 AM)DeaD_EyE Wrote: Try:
print (binascii.unhexlify(a).decode())
Then it's encoded by default as utf-8.
You'll get a different result.

Yes. In my case, this is enough
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  manually input data jpatierno 0 315 Nov-10-2023, 02:32 AM
Last Post: jpatierno
  User serial/pyserial to send messages to an arudino via terminal manually bkapadia 2 2,659 Mar-10-2021, 11:26 AM
Last Post: Larz60+
  How to manually define color bar scale in seaborn heatmap SriRajesh 3 18,172 Sep-08-2019, 11:12 AM
Last Post: RudraMohan
  how to clear the console when the programm is running (not manually) hello_its_me 5 5,657 Sep-30-2017, 09:14 PM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020