[ale] Perl pack question
Ed Cashin
ecashin at noserose.net
Fri Feb 15 13:54:15 EST 2013
These three programs print 12345679 when you pipe packinput to pack to
packoutput as shown in the comment for packoutput.c.
/* packinput.c */
#include <unistd.h>
int main(void)
{
int i = 0x12345678;
return write(STDOUT_FILENO, &i, sizeof i) != sizeof i;
}
#! /opt/local/bin/perl -w
# pack
use strict;
&go;
sub go {
# read "packed" binary data
my $data = do { local $/; <STDIN> };
my $i = unpack("i", $data);
print pack("i", $i + 1);
}
/* packoutput.c
(set -xe
for i in in out; do
gcc -Wall -W pack${i}put.c -o pack${i}put
done
./packinput | ./pack | ./packoutput)
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(void)
{
int i;
if (read(STDIN_FILENO, &i, sizeof i) == sizeof i)
printf("%x\n", i);
else
exit(EXIT_FAILURE);
return 0;
}
On Fri, Feb 15, 2013 at 9:32 AM, Geoffrey Myers <lists at serioustechnology.com
> wrote:
> So I'm trying to modify a binary file with perl. All is going well except
> one issue. I need to change an integer value. I can successfully retrieve
> the value by using:
>
> unpack("S", $var);
>
> I assumed, incorrectly that I could do this to reset the value:
>
> pack("S", $newvar);
>
> Not working. Suggestions?
>
> --
> From my iPhone
> Geoffrey Myers
> _______________________________________________
> Ale mailing list
> Ale at ale.org
> http://mail.ale.org/mailman/listinfo/ale
> See JOBS, ANNOUNCE and SCHOOLS lists at
> http://mail.ale.org/mailman/listinfo
>
--
Ed Cashin <ecashin at noserose.net>
http://noserose.net/e/
http://www.coraid.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.ale.org/pipermail/ale/attachments/20130215/56a01433/attachment.html>
More information about the Ale
mailing list