Showing posts with label binary file. Show all posts
Showing posts with label binary file. Show all posts

Saturday, November 10, 2012

Python open and read integers from binary file

How embarassing that I had to look this up. Here is the syntax in python for opening a binary file:

f = open("filename", "rb")
.
.
f.close()

From this example:

http://stackoverflow.com/questions/4971413/how-to-read-a-binary-file

This was part of a larger search where I was working on pulling values out of a (packet, obviously) binary file. The horrific answer was to read bytes from the file into a "string", then bust out each 8-bit (or other size) value as an integer. So what I ended up doing was reading in 6 "characters", yanking out the packet size from this primary header, and then using that value to read in another "string" of that many characters + 1.

Here is one example site, in which the read() command is used and the result is cast to different sizes of integers:

http://stackoverflow.com/questions/1163459/reading-integers-from-binary-file-in-python

Here is another example where bytes are read in one or chunksize at a time with a loop:

http://stackoverflow.com/questions/1035340/reading-binary-file-in-python

Monday, September 24, 2012

Splitting binary file in two

All answers to this question involved using unix. No surprise.

Early hunts for an answer to this question involved the "split" command. However, split seems to be geared for cutting files into evenly sized pieces only, and not a single arbitrarily placed cut.

That's okay, because "dd" is my new best buddy. It's loaded with arguments, all of which have a lovely archaic format, and can do any number of cuts anywhere. The version of this command that I will henceforth use until the grave is any variation of the following:

for first half:
dd bs=1st location after split count=1 if=infile of=outfile
for last half
dd bs=1st location after split skip=1 if=infile of=outfile

The main hints were found in this forum thread:

http://unix.stackexchange.com/questions/6852/best-way-to-remove-bytes-from-the-start-of-a-file

This other thread is nearly all above my head; neat shmott shtuff, but this where I first got a clue of the existence of dd. I wonder what xxd is?:

http://stackoverflow.com/questions/9451890/how-to-dump-part-of-binary-file