i'm trying to convert ASCII values in string to normal characters, but whatever i'll try to do, i'll get output like
this\032is\032my\032\output\000
and I need
this is my output
Thanks for your help!
I solved it with
import re
def replace(match):
return chr(int(match.group(1)))
aux = str(element.text)
regex = re.compile(r"\\(\d{1,3})")
new = regex.sub(replace, aux)
where element.text is string i need to convert
\032 is octal for the decimal number 26, so it is not in fact a space character, if you use a hex value with the correct number, you should get what you want. 0x20 == 32 below
>>> s = 'this\x20is\x20my\x20\output\00'
>>> print(s)
this is my \output
Related
I'm trying to remove one character from a string o hex values but I can't find a solution to make this work. If I print remove2 I get the string "\x41", but when I print buffer I get ABCD". The thing is I don't understand why when I print remove2 I get the string hex format and when I print buffer I get the ASCII format. I think it is in the root of the problem. How could I fix this using Python 2?
>>> buffer = "\x41\x42\x43\x44"
>>> remove = raw_input("Enter hex value to remove: ")
Enter hex value to remove: 42
>>> remove2 = "\\x" + remove
>>> print buffer
ABCD
>>> print remove2
\x42
>>> buffer2 = buffer.replace(remove2, '')
>>> print buffer2
ABCD
I wish buffer2 = "\x41\x43\x44".
Here's the problem:
remove2 = "\\x" + remove
You can't programmatically build escape sequences like that. Instead, do this:
remove2 = chr(int(remove, 16))
Alternatively, you'd have to make buffer contain the literal backslashes instead of the escaped characters:
buffer = "\\x41\\x42\\x43\\x44"
The problem being is that if you print out remove without the print, you'll see
>>> remove2
'\\x42'
that the \ is staying there and not making it hexadecimal. For that you need to do:
remove.decode('hex')
so the code being:
>>> buffer = "\x41\x42\x43\x44"
>>> remove = raw_input("Enter hex value to remove: ")
Enter hex value to remove: 42
>>> remove2=remove.decode('hex')
>>> buffer.replace(remove2, '')
'ACD'
Does that help/answers your question?
You will need to escape the \ in your buffer string o/w it will be treated as hex value. So,
>>> buffer="\\x41\\x42\\x43"`<br>
>>> remove = "42"`<br>
>>> remove = "\\x" + remove` <br>
>>> buffer = buffer.replace(remove, '')` <br>
>>> print buffer #prints \\\x41\\\x43
You can use filter() and construct a filtered bytes object using the user input of "42" and original bytes (just a string in Python2).
>>> inp = "42"
>>> filter(lambda x: x != chr(int(inp, 16)), 'ABCD')
'ACD'
Python 3
>>> inp = "42"
>>> bytes(filter(lambda x: x != int(inp, 16), b'ABCD'))
b'ACD'
Anyway, simpler to use replace(), this is just an alternative way to filter out specific values from a bytes object. It illustrates the basic idea other answers point out. The user input needs to be correctly converted to the value you intend to remove.
When the interp renders the output, the backslashes aren't represented in bytes or str objects for characters/values that correspond to utf-8 or ascii printable characters. If there isn't a corresponding printable character, then an escaped version of the value will be presented in output.
How can I convert a string x='0x67c31080115dDfeBa0474B3893b2caB1d567438f' into hex to be x=0x67c31080115dDfeBa0474B3893b2caB1d567438f ?
I want to use the same value of the hex but not in a string format
In other word, how can I treat x as a hex value not a string type ( without ' ' )
Thanks
There is no such thing as "hex value". 0x67c31080115dDfeBa0474B3893b2caB1d567438f belongs to type int. You can convert your string to int just with:
x = int(x, 16)
If, however, you want to print it as hex, either use:
print(hex(x))
or
print("{:x}".format(x))
(the first one adds "0x" to the beginning of the string, the other one does not).
You could do it using ast.literal_eval:
import ast
x='0x67c31080115dDfeBa0474B3893b2caB1d567438f'
hex_val = hex(ast.literal_eval(x))
print(hex_val)
My code looks like this :
import struct
str = "AAAAAAA"
len = len(str)+32
package = struct.pack("!H",len)
print repr(package)
the result is :
"\x00'"
When I use len = len(str)
the result is \x00\x07
Why when len is larger than 32,it is not working?
You're misunderstanding the "\x00'" result. It's a mixture of a string hexadecimal character code value and a regular printable ASCII character. If it were displayed purely in hexadecimal character codes, it would be "\x00x\x27".
The \x27 in decimal is the integer 39, which is the result of len(str)+32. It's also the character code of the ' (single quote) character, which is part of what repr() is displaying.
What is the most 'Pythonic' way of translating
'\xff\xab\x12'
into
'ffab12'
I looked for functions that can do it, but they all want to translate to ASCII (so '\x40' to 'a'). I want to have the hexadecimal digits in ASCII.
There's a module called binascii that contains functions for just this:
>>> import binascii
>>> binascii.hexlify('\xff\xab\x12')
'ffab12'
>>> binascii.unhexlify('ffab12')
'\xff\xab\x12'
original = '\xff\xab\x12'
result = original.replace('\\x', '')
print result
It's \x because it's escaped. a.replace(b,c) just replaces all occurances of b with c in a.
What you want is not ascii, because ascii translates 0x41 to 'A'. You just want it in hexadecimal base without the \x (or 0x, in some cases)
Edit!!
Sorry, I thought the \x is escaped. So, \x followed by 2 hex digits represents a single char, not 4..
print "\x41"
Will print
A
So what we have to do is to convert each char to hex, then print it like that:
res = ""
for i in original:
res += hex(ord(i))[2:].zfill(2)
print res
Now let's go over this line:
hex(ord(i))[2:]
ord(c) - returns the numerical value of the char c
hex(i) - returns the hex string value of the int i (e.g if i=65 it will return 0x41.
[2:] - cutting the "0x" prefix out of the hex string.
.zfill(2) - padding with zeroes
So, making that with a list comprehension will be much shorter:
result = "".join([hex(ord(c))[2:].zfill(2) for c in original])
print result
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'