Home: Perl Programming Help: Intermediate:
reading binary data and writing it as hex



talor
New User

Jun 24, 2009, 11:42 PM


Views: 384
reading binary data and writing it as hex

Hi,



I have a binary data file: 43 2F FF A2 2B 12 99 4C... (each pair represents a byte)

The user is supposed to give me a pattern: 0000_###_0 (each '#' represents a byte)

And I want to ouput:

0000_432fff_0

0000_a22b12_0

etc.

The pattern isn't fixed, so the number of '#' may change (though they have to be consecutive).

Let's say I already read the amount of bytes requestsed from the data file, is there a way to use 'unpack' to output such a hex number (which isn't the size of an int or long necessarily)?

BTW, I could do it by writing one byte at a time but the data files I'm dealing with are very big and I'm looking for a faster way to do it... (specifically to unpack into a variable and writing it with printf and %x).



Thanks a lot!


1arryb
User

Jun 25, 2009, 12:57 PM


Views: 377
Re: [talor] reading binary data and writing it as hex

Hi talor,

You can do pretty much whatever you want with pack and unpack. Why don't you read the manual on those functions, try a few things, and come back with some test code and your (more specific) questions?

Good luck,

Larry