Reading a raw binary data in python and converting it into ascii

17,780

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]
Share:
17,780
Vincent
Author by

Vincent

Updated on June 05, 2022

Comments

  • Vincent
    Vincent almost 2 years

    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?