in reply to Write 2 uint16_t numbers to file in Perl
You are unpacking a binary representation of a bunch of unsigned shorts ($hdr) into an array of perl scalars (@rgb), then replacing two elements in that array with packed unsigned shorts, which isn't what you want. Just substitute the new values directly into the array:
$rgb[16] = 7360; # Overwrite 3689 with 7360 $rgb[22] = 4912; # 2462 -> 4912
Now if you want to write this scalar array back to a binary file, try:
open(my $outfile, '>:raw', $hfn) or die "Couldn't open $hfn: $!\n"; # Overwrites existing file print $outfile pack("S*", @rgb); close($outfile) or die "Couldn't close $hfn: $!\n";
(The :raw modifier on the second argument of open is equivalent to using binmode).
If you know beforehand the positions and values in the binary file you need to change, you can save yourself the trouble of reading it all in to memory, unpacking everything to an array and re-packing it all at the end by just editing it in place:
open(my $fh, '+<:raw', $hfn) or die "Couldn't open $hfn: $!\n"; seek($fh, 2*16, 0); # Double up 'cause 'seek' wants bytes not short +ints print $fh pack('S', 7360); seek($fh, 2*22, 0); print $fh pack('S', 4912); close($fh) or die "Couldn't close $hfn: $!\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Write 2 uint16_t numbers to file in Perl
by AnomalousMonk (Archbishop) on Jan 05, 2016 at 18:54 UTC | |
by Anonymous Monk on Jan 05, 2016 at 21:06 UTC |