Hi
I have this key
How can I add \x between two number of hex as you see in my example.

f0cbf260e0ca8ec2431089fb393a1c29513aaaa5847d13e8be84760968e64dc6

Change to

“\xf0\xcb\xf2\x60\xe0\xca\x8e\xc2\x43\x10\x89\xfb\x39\x3a\x1c\x29\x51\x3a\xaa\xa5\x84\x7d\x13\xe8\xbe\x84\x76\x09\x68\xe6\x4d\xc6”

I appreciate your help

Recommended Answers

All 16 Replies

Hi. Use module binascii

>>> from binascii import unhexlify
>>> s = 'f0cbf260e0ca8ec2431089fb393a1c29513aaaa5847d13e8be84760968e64dc6'
>>> t = unhexlify(s)
>>> t
'\xf0\xcb\xf2`\xe0\xca\x8e\xc2C\x10\x89\xfb9:\x1c)Q:\xaa\xa5\x84}\x13\xe8\xbe\x84v\th\xe6M\xc6'

THanks Gribouillis
why we get )Q: ,} and \th\xe6M ?

Sequences such as \xf0 represent a single byte (8 bits. Remark: in python 3, use b'\xf0').

Some bytes are better viewed as ascii characters. For example 'Q' and '\x51' are the same character. In fact, 'Q' is the character number 81 in ascii, and 81 rewritten in hexadecimal system is 51, (5*16 + 1). See https://en.wikipedia.org/wiki/ASCII .

Python displays printable characters in ascii form and other characters in hex form.

Edit: some special characters are escaped too, '\t' is a tabulation character (number 9, or '\x09').

Another way using the slice operator ...

s = "f0cbf260e0ca8ec2431089fb393a1c29513aaaa5847d13e8be84760968e64dc6"
sx = r"\x" + r"\x".join(s[n : n+2] for n in range(0, len(s), 2))

print(sx)

Wow vegaseat
work very fine
Thanks

Vegaseat's method does not work. It gives another string:

  '\\xf0\\xcb\\xf2\\x60\\xe0\\xca\\x8e\\xc2\\x43\\x10\\x89\\xfb\\x39\\x3a\\x1c\\x29\\x51\\x3a\\xaa\\xa5\\x84\\x7d\\x13\\xe8\\xbe\\x84\\x76\\x09\\x68\\xe6\\x4d\\xc6'

Mm
Here the keys

f0cbf260e0ca8ec2431089fb393a1c29513aaaa5847d13e8be84760968e64dc6
9b18635534875fc2ba1a74616e961caaaa907d8b285c7625bb44eb256b8de59d
000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f

The source is here

http://mweissbacher.com/blog/tag/truecrypt/

can you check it if you get right?

Gribouillis ,Vegaseat's method gives me right output

s = "f0cbf260e0ca8ec2431089fb393a1c29513aaaa5847d13e8be84760968e64dc6"
sx = r"\x" + r"\x".join(s[n : n+2] for n in range(0, len(s), 2))
print(sx)


#Output
#\xf0\xcb\xf2\x60\xe0\xca\x8e\xc2\x43\x10\x89\xfb\x39\x3a\x1c\x29\x51\x3a\xaa\xa5\x84\x7d\x13\xe8\xbe\x84\x76\x09\x68\xe6\x4d\xc6

We have to just add "" in the begning and in the end.

It is not the right output. Consider the following example

>>> from binascii import unhexlify
>>> s = "f0"
>>> A = unhexlify(s)
>>> B = r"\x" + r"\x".join(s[n : n+2] for n in range(0, len(s), 2))
>>> print(A)
�
>>> print(B)
\xf0
>>> print(repr(A))
'\xf0'
>>> print(repr(B))
'\\xf0'
>>> print(len(A))
1
>>> print(len(B))
4

The string A constructed with unhexlify() contains a single byte, which is '\xf0', or the 8 bits 11110000 (it does not print well in a terminal because it is not a printable character). String B constructed with vegaseat's expression contains 4 bytes, namely the characters \, x, f, 0. Its bitwise representation is 01011100011110000110011000110000.

See this post https://www.daniweb.com/software-development/python/code/221031/string-to-bits#post1484218 for bit representations of byte strings.

Edit: are you the blog's author ?

Thanks
No,I'm not blog's author but working with IT-security.

What you want is a byte string like
sb1 = b"\xf0\xcb\xf2\x60\xe0\xca\x8e\xc2\x43\x10\x89\xfb\x39\x3a\x1c\x29"

Yes
here the keys

f0cbf260e0ca8ec2431089fb393a1c29513aaaa5847d13e8be84760968e64dc69b18635534875fc2ba1a74616e961caaaa907d8b285c7625bb44eb256b8de59d000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f

You can look at the Patching TrueCrypt
http://mweissbacher.com/blog/tag/truecrypt/

 buffer[64] = ‘\x00′;
+                       memcpy(buffer, “\xf0\xcb\xf2\x60\xe0\xca\x8e\xc2\x43\x10\x89\xfb\x39\x3a\x1c\x29\x51\x3a\xaa\xa5\x84\x7d\x13\xe8\xbe\x84\x76\x09\x68\xe6\x4d\xc6\x9b\x18\x63\x55\x34\x87\x5f\xc2\xba\x1a\x74\x61\x6e\x96\x1c\xaa\xaa\x90\x7d\x8b\x28\x5c\x76\x25\xbb\x44\xeb\x25\x6b\x8d\xe5\x9d”, 64);

Unless you know the codecs these are not printable characters ...

""" byte_string_test102.py
see also Python module codecs
"""

# byte string in 4 parts to show it better
bs1 = b"\xf0\xcb\xf2\x60\xe0\xca\x8e\xc2\x43\x10\x89\xfb\x39\x3a\x1c\x29"
bs2 = b"\x51\x3a\xaa\xa5\x84\x7d\x13\xe8\xbe\x84\x76\x09\x68\xe6\x4d\xc6"
bs3 = b"\x9b\x18\x63\x55\x34\x87\x5f\xc2\xba\x1a\x74\x61\x6e\x96\x1c\xaa"
bs4 = b"\xaa\x90\x7d\x8b\x28\x5c\x76\x25\xbb\x44\xeb\x25\x6b\x8d\xe5\x9d"
# total byte atring
bs = bs1 + bs2 + bs3 + bs4

for byte in bs:
    print("{0:x} --> {0:d}".format(byte))

Here is an example where I knew the codec (latin_1) ...

# a simple byte string
bs = b'\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef'

# decode Python3 byte string to a string
s = bs.decode("latin_1")

print(bs)
print(s)

''' result (Python34) ...
b'\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef'
àáâãäåæçèéêëìíîï
'''

Thanks vegaseat

My friend.
I sugest you to follow this site that has a lot of Python's values conversion tips.
Including that about HEX format.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.