0

(Answer found. Close the topic)

I'm trying to convert hex values, stored as string, in to hex data.

I have:

data_input = 'AB688FB2509AA9D85C239B5DE16DD557D6477DEC23AF86F2AABD6D3B3E278FF9'

I need:

data_output = '\xAB\x68\x8F\xB2\x50\x9A\xA9\xD8\x5C\x23\x9B\x5D\xE1\x6D\xD5\x57\xD6\x47\x7D\xEC\x23\xAF\x86\xF2\xAA\xBD\x6D\x3B\x3E\x27\x8F\xF9'

I was trying data_input.decode('hex'), binascii.unhexlify(data_input) but all they return:

"\xabh\x8f\xb2P\x9a\xa9\xd8\\#\x9b]\xe1m\xd5W\xd6G}\xec#\xaf\x86\xf2\xaa\xbdm;>'\x8f\xf9"

What should I write to receive all bytes in '\xFF' view?


updating:

I need representation in '\xFF' view to write this data to a file (I'm opening file with 'wb') as:

«hЏІPљ©Ш\#›]бmХWЦG}м#Ї†тЄЅm;>'Џщ

update2

Sorry for bothering. An answer lies under my nose all the time:

data_output = data_input.decode('hex')
write_file(filename, data_output)  #just opens a file 'wb', ant write a data in it

gives the same result as I need

6
  • 3
    You appear to be confusing the Python representation with the contents. Python merely gives you ASCII characters where the bytes can be interpreted as such. \x68 is the ASCII codepoint for the letter a, so Python presents you with that as such. Why do you have to have it printed as \x68 instead? Commented Aug 29, 2014 at 14:39
  • 2
    If you print the data_output value as specified, it'll print exactly the same way as the output of data_input.decode('hex'). In other words, data_output == data_input.decode('hex') is True. Did you want to output literal backslashes and x characters plus two hex characters instead? Commented Aug 29, 2014 at 14:40
  • @MartijnPieters FWIW, \x68 is the h not the a. Commented Aug 29, 2014 at 14:53
  • @sebastian: ah, yes, of course. Commented Aug 29, 2014 at 15:02
  • 1
    Welcome to Stack Overflow, Mike. Your question is somewhat unclear, but could be made much more clear if you'd provide a complete sample program. Please create a short, complete program that demonstrates the problem you are having. Please include the output you see and the output you desire. See stackoverflow.com/help/mcve and SSCCE.org for more info. Commented Aug 29, 2014 at 15:03

3 Answers 3

2

I like chopping strings into fixed-width chunks using re.findall

print '\\x' + '\\x'.join(re.findall('.{2}', data_input))

If you want to actually convert the string into a list of ints, you can do that like this:

data = [int(x, 16) for x in re.findall('.{2}', data_input)]
Sign up to request clarification or add additional context in comments.

Comments

1

It's an inefficient solution, but there's always:

flag = True
data_output = ''
for char in data_input:
    if flag:
        buffer = char
        flag = False
    else:
        data_output = data_output + '\\x' + buffer + char
        flag = True

EDIT HOPEFULLY THE LAST: Who knew I could mess up in so many different ways on that simple a loop? Should actually run now...

8 Comments

I'm getting NameError: name 'true' is not defined on the first line.
@Kevin My bad, forgot boolean capitalization (working on java in another tab, confused myself)
I'm aware it's non-ideal, but it should function, which is better than nothing.
A different take on the same solution: ''.join(['\\x'+x+y for x,y in zip(data_output[::2], data_output[1::2])])
A different different take: "".join("\\x{:2X}".format(ord(c)) for c in data_input.decode("hex"))
|
0
>>> int('0x10AFCC', 16)
1093580
>>> hex(1093580)
'0x10afcc'

So prepend your string with '0x' then do the above

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.