In Python, how can I represent an integer value (<256) as a string? For example:
i = 10
How can I create a string "s" that is one-byte long, and the byte has the value 10?
to clarify, I do not want a string "10". I want a string that its 1st (and only) byte has the value of 10.
by the way, I cannot create the string statically:
s = '\x0A'
because the value is not pre-defined. It is a dynamic number value.
You can use chr() function as:
>>> chr(60)
'<'
>>> chr(97)
'a'
>>> chr(67)
'C'
To convert back use ord() funtion as:
>>> ord('C')
67
In Python 2.x, you want:
s = chr(10)
In Python 3.x, strings are Unicode, so you want:
s = bytes([10])
why don't you just use chr?
chr(10)
Out[41]: '\n'
chr(255)
Out[42]: '\xff'
Found another answer using struct module working for Python 2:
import struct
struct.pack('B', i)
Related
I want to convert a hex string to utf-8
a = '0xb3d9'
to
동 (http://www.unicodemap.org/details/0xB3D9/index.html)
First, obtain the integer value from the string of a, noting that a is expressed in hexadecimal:
a_int = int(a, 16)
Next, convert this int to a character. In python 2 you need to use the unichr method to do this, because the chr method can only deal with ASCII characters:
a_chr = unichr(a_int)
Whereas in python 3 you can just use the chr method for any character:
a_chr = chr(a_int)
So, in python 3, the full command is:
a_chr = chr(int(a, 16))
I'm reading a wav audio file in Python using wave module. The readframe() function in this library returns frames as hex string. I want to remove \x of this string, but translate() function doesn't work as I want:
>>> input = wave.open(r"G:\Workspace\wav\1.wav",'r')
>>> input.readframes (1)
'\xff\x1f\x00\xe8'
>>> '\xff\x1f\x00\xe8'.translate(None,'\\x')
'\xff\x1f\x00\xe8'
>>> '\xff\x1f\x00\xe8'.translate(None,'\x')
ValueError: invalid \x escape
>>> '\xff\x1f\x00\xe8'.translate(None,r'\x')
'\xff\x1f\x00\xe8'
>>>
Any way I want divide the result values by 2 and then add \x again and generate a new wav file containing these new values. Does any one have any better idea?
What's wrong?
Indeed, you don't have backslashes in your string. So, that's why you can't remove them.
If you try to play with each hex character from this string (using ord() and len() functions - you'll see their real values. Besides, the length of your string is just 4, not 16.
You can play with several solutions to achieve your result:
'hex' encode:
'\xff\x1f\x00\xe8'.encode('hex')
'ff1f00e8'
Or use repr() function:
repr('\xff\x1f\x00\xe8').translate(None,r'\\x')
One way to do what you want is:
>>> s = '\xff\x1f\x00\xe8'
>>> ''.join('%02x' % ord(c) for c in s)
'ff1f00e8'
The reason why translate is not working is that what you are seeing is not the string itself, but its representation. In other words, \x is not contained in the string:
>>> '\\x' in '\xff\x1f\x00\xe8'
False
\xff, \x1f, \x00 and \xe8 are the hexadecimal representation of for characters (in fact, len(s) == 4, not 24).
Use the encode method:
>>> s = '\xff\x1f\x00\xe8'
>>> print s.encode("hex")
'ff1f00e8'
As this is a hexadecimal representation, encode with hex
>>> '\xff\x1f\x00\xe8'.encode('hex')
'ff1f00e8'
By hex-string, it is a regular string except every two characters represents some byte, which is mapped to some ASCII char.
So for example the string
abc
Would be represented as
979899
I am looking at the binascii module but don't really know how to take the hex-string and turn it back into the ascii string.
Which method can I use?
Note: I am starting with 979899 and want to convert it back to abc
You can use ord() to get the integer value of each character:
>>> map(ord, 'abc')
[97, 98, 99]
>>> ''.join(map(lambda c: str(ord(c)), 'asd'))
'979899'
>>> ''.join((str(ord(c)) for c in 'abc'))
'979899'
You don't need binascii to get the integer representation of a character in a string, all you need is the built in function ord().
s = 'abc'
print(''.join(map(lambda x:str(ord(x)),s))) # outputs "979899"
To get the string back from the hexadecimal number you can use
s=str(616263)
print "".join([chr(int(s[x:x+2], 16)) for x in range(0,len(s),2)])
See http://ideone.com/dupgs
I have a hex string like:
data = "437c2123"
I want to convert this string to a sequence of characters according to the ASCII table.
The result should be like:
data_con = "C|!#"
Can anyone tell me how to do this?
In Python2
>>> "437c2123".decode('hex')
'C|!#'
In Python3 (also works in Python2, for <2.6 you can't have the b prefixing the string)
>>> import binascii
>>> binascii.unhexlify(b"437c2123")
b'C|!#'
In [17]: data = "437c2123"
In [18]: ''.join(chr(int(data[i:i+2], 16)) for i in range(0, len(data), 2))
Out[18]: 'C|!#'
Here:
for i in range(0, len(data), 2) iterates over every second position in data: 0, 2, 4 etc.
data[i:i+2] looks at every pair of hex digits '43', '7c', etc.
chr(int(..., 16)) converts the pair of hex digits into the corresponding character.
''.join(...) merges the characters into a single string.
Since Python 2.6 you can use simple:
data_con = bytes.fromhex(data)
The ord function converts characters to numerical values and the chr function does the inverse. So to convert 97 to "a", do ord(97)
For example, I get a string:
str = "please answer my question"
I want to write it to a file.
But I need to know the size of the string before writing the string to the file. What function can I use to calculate the size of the string?
If you are talking about the length of the string, you can use len():
>>> s = 'please answer my question'
>>> len(s) # number of characters in s
25
If you need the size of the string in bytes, you need sys.getsizeof():
>>> import sys
>>> sys.getsizeof(s)
58
Also, don't call your string variable str. It shadows the built-in str() function.
Python 3:
user225312's answer is correct:
A. To count number of characters in str object, you can use len() function:
>>> print(len('please anwser my question'))
25
B. To get memory size in bytes allocated to store str object, you can use sys.getsizeof() function
>>> from sys import getsizeof
>>> print(getsizeof('please anwser my question'))
50
Python 2:
It gets complicated for Python 2.
A. The len() function in Python 2 returns count of bytes allocated to store encoded characters in a str object.
Sometimes it will be equal to character count:
>>> print(len('abc'))
3
But sometimes, it won't:
>>> print(len('йцы')) # String contains Cyrillic symbols
6
That's because str can use variable-length encoding internally. So, to count characters in str you should know which encoding your str object is using. Then you can convert it to unicode object and get character count:
>>> print(len('йцы'.decode('utf8'))) #String contains Cyrillic symbols
3
B. The sys.getsizeof() function does the same thing as in Python 3 - it returns count of bytes allocated to store the whole string object
>>> print(getsizeof('йцы'))
27
>>> print(getsizeof('йцы'.decode('utf8')))
32
>>> s = 'abcd'
>>> len(s)
4
You also may use str.len() to count length of element in the column
data['name of column'].str.len()
The most Pythonic way is to use the len(). Keep in mind that the '\' character in escape sequences is not counted and can be dangerous if not used correctly.
>>> len('foo')
3
>>> len('\foo')
3
>>> len('\xoo')
File "<stdin>", line 1
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: truncated \xXX escape
Do you want to find the length of the string in python language ? If you want to find the length of the word, you can use the len function.
string = input("Enter the string : ")
print("The string length is : ",len(string))
OUTPUT : -
Enter the string : viral
The string length is : 5