2

I have a raw binary data and I want to convert it into a readable text.

the text contains with something that is not readable, it has also special characters, like black box with NUL word or " N–[«´N–[« )› )ÿ " . I'm just new in python.

here's my code

import struct
file = open('rawbinary.txt')
text = file.read()
struct.unpack("iiiii", text[:20])

my output was:

(2113933569, 67305475, -80477197, 1536577129, 1312228259)

and if add this:

text[:10]

my output is

'\x01\x11\x00~\x03\x00\x03\x04\xf3\x03'

Am I doing it right? What is my next step?

3
  • 1
    what's your OS, you should open the file with 'rb' on windows (linux does not need the binary differentiation). Commented Oct 21, 2013 at 3:56
  • 1
    can you gist/fiddle/pastebin...etc your rawbinary.txt file, what do you expect to see? your format string is 'iiii' which means format as integer. docs.python.org/2/library/struct.html (also, change to open('rawbinary.txt', 'rb') for consistent behavior on windows) Commented Oct 21, 2013 at 4:06
  • i want to see it in ascii form. Commented Oct 21, 2013 at 5:17

1 Answer 1

5

Use the built-in ord function.

with open("/bin/ls", "rb") as fin:
  buf = fin.read()
bytes = map(ord, buf)    
print bytes[:10]

output:

[127, 69, 76, 70, 2, 1, 1, 0, 0, 0]
Sign up to request clarification or add additional context in comments.

3 Comments

can this be outputted into ascii?
i mean. into letters.
No. Some of the byte codes (e.g. anything less than 65) correspond to symbols that are not letters.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.