0

Firstly, I appreciate any help you're willing to provide, so thanks for taking the time to read this. Also, I'm using python 3, but I'll do my best to convert any python 2 syntax for my needs.

I have managed to read my binary file to produce a very long string. Here's a small piece of it:

myfile = "\xde\xad\xbe\xef\x01\x00\xe1\x07\x01\x00\x01\x00\x1e\x00\"

I am trying to convert this to ASCII text. The first thing I'm struggling with is the backslashes. I have tried the following:

convert1 = myfile.split('\\\')
print(convert1)

This gives me:

['xde', 'xad', 'xbe', 'xef', 'x01', 'x00', 'xe1', 'x07', 'x01', 'x00', 'x01', 'x00']

The problem is I've lost my backslashes. I would like to have it output:

['\xde', '\xad', '\xbe', '\xef', '\x01', '\x00', '\xe1', '\x07', '\x01', '\x00', '\x01', '\x00']

From here I intend on using a for loop to convert each item in the list to an ordinal, and then later back to a character:

convert2 = []
for items in convert1:
    i = ord(items)
    convert2.append(i)
convert3 = []
for items in convert2:
    i = chr(items)
    convert3.append(i)

I hope this will give me the ASCII characters, but if you know an easier way, I'd love to hear it. Thanks in advance for your help.

Matthew Reid
  • 332
  • 2
  • 9
  • http://stackoverflow.com/questions/7396849/convert-binary-to-ascii-and-vice-versa – Dadep Feb 21 '17 at 12:08
  • It's not completely clear what you are trying to do. Why do you believe the binary file data is convertible to ASCII text? Is the sample you've shown from the beginning of the file? The byte string `b"\xde\xad\xbe\xef\x01\x00\xe1\x07\x01\x00\x01\x00\x1e\x00\"` does not look like a simple encoding of text to me. – PM 2Ring Feb 21 '17 at 12:17
  • 2
    Neither your `myfile = ` nor your `convert = ` lines are valid Python. If we remove the last backslash, then you won't get what you say you do, because (for example) the string `"\xde"` doesn't have any literal backslashes in it (it has length 1, not length 4.) – DSM Feb 21 '17 at 12:29
  • Good points, @DSM.It would be helpful to know how Matt created that `myfile` string. – PM 2Ring Feb 21 '17 at 12:49

1 Answers1

0

Your myfile code is not a valid Python line. Assuming we remove the last \ it works. If you want to split it into each individual character using ord() a simple list comprehension does it.

myfile = "\xde\xad\xbe\xef\x01\x00\xe1\x07\x01\x00\x01\x00\x1e\x00"
convert = [ ord( item ) for item in myfile ]
print( convert )

Output: [222, 173, 190, 239, 1, 0, 225, 7, 1, 0, 1, 0, 30, 0]

Each character of the byte string ( "\xde", "\xad", ... ) are counted as individual units in themselves so iterating over the byte string will already count them as individual units, no need to split it on the slashes at all. All you have to do next is apply ord() onto each of those units while iterating to get the result you want.

Meghdeep Ray
  • 5,262
  • 4
  • 34
  • 58
  • That worked well for the conversion I was trying to achieve. Thanks for all your help. I think the file I'm trying to convert is not easily convertible from binary to ASCII, but the string conversion method you showed works well. Thanks again. Matt. – Matthew Reid Feb 22 '17 at 21:38