Please try this code
import binascii
mytext='☺00AB12☻0200D60000040001♥t'
print binascii.hexlify(mytext)
I've received this output
3f3030414231323f303230304436303030303034303030313f74
Answer from Eugene on Stack OverflowUse str.replace to replace that character with the hex representation of its ordinal value. And to get the actual string back you can decode it using string-decode.
>>> s = 'banana'
>>> print s.replace('a', '\\x' + format(ord('a'), 'x'))
b\x61n\x61n\x61
>>> print s.replace('a', '\\x' + format(ord('a'), 'x')).decode('string-escape')
banana
To do it and keep the encoding as standard ASCII not hex...
import re
s = 'banana'
t = s.replace('a', '\\x{}'.format(ord('a')))
subs = re.findall(r'\\x\d{2}',t)
decoded = ""
for match in set(subs):
decoded = t.replace(match, chr(int(match[2:4]))
print decoded
Some more clever google-fu uncovered this code snippet, which gives me the results I expect:
def convertMac(octet):
mac = [binascii.b2a_hex(x) for x in list(octet)]
return ":".join(mac)
So "decoding" is really a misnomer, because this code also yields a proper result (with encode and decode appearing to be syntactic sugar for binascii.a2b() and binascii.b2a()):
Python 2.7.3 (default, Apr 10 2013, 06:20:15)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> string = '\x00$8\xa1\x1c2'
>>> string.encode("hex")
'002438a11c32'
>>>
Change "hex" to "hex_codec" for Python < 3
This applies to python 3+ Possible to use?
>>> codecs.decode(b"4f6c6567", "hex_codec")
b'Oleg'
>>> codecs.getdecoder("hex_codec")(b"4f6c6567")
(b'Oleg', 8)
You can transform your string to an integer generator. Apply hexadecimal formatting for each element and intercalate with a separator:
>>> s = "Hello, World!"
>>> ":".join("{:02x}".format(ord(c)) for c in s)
'48:65:6c:6c:6f:2c:20:57:6f:72:6c:64:21
':'.join(x.encode('hex') for x in 'Hello, World!')
Try this:
hex_str = "0xAD4"
hex_int = int(hex_str, 16)
new_int = hex_int + 0x200
print hex(new_int)
If you don't like the 0x in the beginning, replace the last line with
print hex(new_int)[2:]
Use int function with second parameter 16, to convert a hex string to an integer. Finally, use hex function to convert it back to a hexadecimal number.
print hex(int("0xAD4", 16) + int("0x200", 16)) # 0xcd4
Instead you could directly do
print hex(int("0xAD4", 16) + 0x200) # 0xcd4
Given that your string contains ascii only (each char is in range 0-0xff), you can use the following snippet:
In [28]: s = '746865'
In [29]: import math
In [30]: int(s, base=16).to_bytes(math.ceil(len(s) / 2), byteorder='big').decode('ascii')
Out[30]: 'the'
Firstly you need to convert a string into integer with base of 16, then convert it to bytes (assuming 2 chars per byte) and then convert bytes back to string using decode
#!/usr/bin/python3
"""
Program name: txt_to_ASC.py
The program transfers
a string of letters -> the corresponding
string of hexadecimal ASCII-codes,
eg. the -> 746865
Only letters in [abc...xyzABC...XYZ] should be input.
"""
print("Transfer letters to hex ASCII-codes")
print("Input range is [abc...xyzABC...XYZ].")
print()
string = input("Input set of letters, eg. the: ")
print("hex ASCII-code: " + " "*15, end = "")
def str_to_hasc(x):
global glo
byt = bytes(x, 'utf-8')
bythex = byt.hex()
for b1 in bythex:
y = print(b1, end = "")
glo = str(y)
return glo
str_to_hasc(string)
Your class.function expects an integer which can be represented either by a decimal or a hexadecimal literal, so that these two calls are completely equivalent:
class.function(0x77)
class.function(119) # 0x77 == 119
Even print(0x77) will show 119 (because decimal is the default representation).
So, we should rather be talking about converting a string representation to integer. The string can be a hexadecimal representation, like '0x77', then parse it with the base parameter:
>>> int('0x77', 16)
119
or a decimal one, then parse it as int('119').
Still, storing integer whenever you deal with integers is better.
EDIT: as @gnibbler suggested, you can parse as int(x, 0), which handles both formats.
>>> hex(119)
'0x77'
#or:
>>> hex(int("119"))
'0x77'
This should work for you.
You can also get the hex representation of characters:
>>> hex(ord("a"))
'0x61'