|Welcome to the Monastery|
Convert binary file to asciiby richz (Beadle)
|on Jun 22, 2007 at 17:57 UTC||Need Help??|
richz has asked for the
wisdom of the Perl Monks concerning the following question:
I have a binary file that looks like the following in a hex editor(these are the first 8 bytes):
02 00 00 00 f7 ff f4 ff ....
What I would like to do is convert every 2 bytes into an ASCII string that I can then write out to a new file. So for the above example, I want my output file to look like this (bytes flipped due to endianness issues):
0x0002, 0x0000, 0xfff7, 0xfff4 What I have found so far is that I need to set the filehandle to binary mode using binmode() after I open it, and then also use ord() to convert a binary value to its ascii equivalent. The problem I have right now is trying to group every set of 2 binary bytes into 4 hex digits but reversed for the endianness reason. My code so far:
As output this will give me:
Any suggestions on how to get the behavior I would like? Furthermore, when I call split on one line of the binary file, will it split the line into individual bytes? This is what it appears to be doing, but then how does ord work exactly? If it sees a binary value of ff what does it convert that to? Thanks.