Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

O Wise Ones,
I am using Image::ExifTool to read a custom (unknown) tag from a TIFF file, and it is returning the following:
Exif 0x866c    : 76 73 0 4 244 1 0 0 0 ...
I need to code this into a series of 32-bit values. For example, the first four values (76,73,0,4) would be expressed in hex as 0x0400494c (note the bytes are reversed). Similarly, the next four values (244,1,0,0) would be expressed in hex as 0x000001f4. I'm currently doing this with some splits:
$offset = join('',reverse map { sprintf '%02x',$_ } (split(' ',$info))[0..4];
then converting to decimal to use as a byte offset:
seek(FILE,hex($offset),0);
Is this really the best way to do it, or is there some pack-fu that I should use? Thanks!

Replies are listed 'Best First'.
Re: Converting string of decimal values from Image::ExifTool
by pc88mxer (Vicar) on Apr 23, 2008 at 20:02 UTC
    The "V" format seems to work:
    my $x = "\x{4c}\x{49}\x{0}\x{4}\x{f4}\1\0\0"; printf "V: %08x %08x\n", unpack("VV", $x), "\n"; __END__ V: 0400494c 000001f4
Re: Converting string of decimal values from Image::ExifTool
by almut (Canon) on Apr 23, 2008 at 20:24 UTC

    There's nothing wrong with your solution, but if you want to use pack, you could do

    my $info = "76 73 0 4 244 1 0 0"; my $bin = pack "C*", reverse split ' ', $info; print join(" ", unpack("(H8)*", $bin) ), "\n"; # hex offsets print join(" ", unpack("N*", $bin) ), "\n"; # decimal offsets

    Output:

    000001f4 0400494c 
    500 67127628
    

    Or, if you only need the decimal offsets

    print join(" ", unpack("V*", pack "C*", split ' ', $info) ), "\n"; # +67127628 500