How to convert string hex into bytes format? [duplicate] - python

This question already has answers here:
Show hex value for all bytes, even when ASCII characters are present
(2 answers)
Closed 3 months ago.
The string is like "e52c886a88b6f421a9324ea175dc281478f03003499de6162ca72ddacf4b09e0", when I run the code, the output is not my expectation, like this.
hexstr = "e52c886a88b6f421a9324ea175dc281478f03003499de6162ca72ddacf4b09e0"
hexstr = bytes.fromhex(hexstr)
print(hexstr)
The output is
b'\xe5,\x88j\x88\xb6\xf4!\xa92N\xa1u\xdc(\x14x\xf00\x03I\x9d\xe6\x16,\xa7-\xda\xcfK\t\xe0'
My expected output should like b'\xe5\x2c\xc8\x86......

Your code is correct.
Python tries to be helpful by displaying bytes that map to an ASCII character as that character. For example, \x2c maps to ,.
>>> b',' == b'\x2c'
True

Related

Printing escape sequence character in python [duplicate]

This question already has answers here:
How to get the ASCII value of a character
(5 answers)
Closed 3 years ago.
I tried to print the escape sequence characters or the ASCII representation of numbers in Python in a for loop.
Like:
for i in range(100, 150):
b = "\%d" %i
print(b)
I expected the output like,
A
B
C
Or something.
But I got like,
\100
\101
How to print ASCII representation of the numbers?
There's a builtin function for python called ord and chr
ord is used to get the value of ASCII letter, for example:
print(ord('h'))
The output of the above is 104
ord only support a one length string
chr is inverse of ord
print(chr(104))
The output of the above is 'h'
chr only supports integer. float, string, and byte doesn't support
chr and ord are really important if you want to make a translation of a text file (encoded text file)
You can use the ord() function to print the ASCII value of a character.
print(ord('b'))
> 98
Likewise, you can use the chr() function to print the ASCII character represented by a number.
print(chr(98))
> b

Python convert unicode with ''.join (TypeError: sequence item 0: expected string, int found) [duplicate]

This question already has answers here:
hex string to character in python
(4 answers)
Closed 6 years ago.
I'm using python 2.7.9 to convert unicode hex to unicode text and I got stuck on following code:
text = '0421'
converted_text = ''.join([chr(int(''.join(c), 16)) for c in zip(text[0::4], text[1::4], text[2::4], text[3::4])])
print converted_text
ValueError: chr() arg not in range(256)
When I remove chr():
converted_text = ''.join([int(''.join(c), 16) for c in zip(text[0::4], text[1::4], text[2::4], text[3::4])])
TypeError: sequence item 0: expected string, int found
If I try other text like '00DD' it works fine.
Any idea what's problem in my code?
If you need unicode character then solution is to use unichr() instead of chr().

Regex string insertion [duplicate]

This question already has answers here:
What exactly do "u" and "r" string prefixes do, and what are raw string literals?
(7 answers)
Closed 6 years ago.
Simple, simple question, hope you can help me:
How do I add a string to a regex?
Say:
d = '\d\d\d'
mo = re.compile(r #d goes here)
Pasting it, separating it with a comma, or with a plus gives me errors.
Normally, as you know, it would be re.compile(r'\d\d\d')
Is this what you are looking for?
d = r"\d\d\d"
re.compile(d)
Maybe more intuitive:
d = r"\d{3}"
# match a digit exactly three times, consecutively
re.compile(d)

python utf-8 behaviour [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Python returning the wrong length of string when using special characters
I read a multilingual string from file in windows-1251, for example s="qwe абв" (second part in Russian), and then:
for i in s.decode('windows-1251').encode('utf-8').split():
print i, len(i)
and I get:
qwe 3
абв 6
Oh God, why? o_O
In programming languages you can't always think of strings as a sequence of characters, because generally they are actually a sequence of bytes. You can't store every character or symbol in 8 bits, character encodings create some rules to combine multiple bytes into a single character.
In the case of the string 'абв' encoded in utf-8, what you have is 6 bytes that represent 3 characters. If you want to count the number of characters instead of the number of bytes, make sure you are taking the length from a unicode string.
>>> print "абв"
абв
>>> print [char for char in "абв"]
['\xd0', '\xb0', '\xd0', '\xb1', '\xd0', '\xb2']
That's why :)

Get the last 4 characters of a string [duplicate]

This question already has answers here:
How do I get a substring of a string in Python? [duplicate]
(16 answers)
Closed 7 years ago.
I have the following string: "aaaabbbb"
How can I get the last four characters and store them in a string using Python?
Like this:
>>> mystr = "abcdefghijkl"
>>> mystr[-4:]
'ijkl'
This slices the string's last 4 characters. The -4 starts the range from the string's end. A modified expression with [:-4] removes the same 4 characters from the end of the string:
>>> mystr[:-4]
'abcdefgh'
For more information on slicing see this Stack Overflow answer.
str = "aaaaabbbb"
newstr = str[-4:]
See : http://codepad.org/S3zjnKoD

Categories