Makistos
10/28/2013 - 8:05 AM

This small script reads a memory dump that looks like 0x00 0xAB 0xFE 0x10 ... and converts it into 16 -254 171 0 ...

This small script reads a memory dump that looks like

0x00 0xAB 0xFE 0x10 ...

and converts it into

16 -254 171 0 ...

I needed this to verify some code as I got two different outputs. Not only was the format different, endianess was as well. Converting from hex to int wasn't the problem, it was making Perl to understand that the values were signed ints, not unsigned.

So, really, only the conversion lines are interesting.

#conversion #hexadecimal-numbers #perl

#!/usr/bin/perl

my $byte_num = 0;
my @vals;

while(<>)
{
	chomp;
	$vals[$byte_num] = unpack('c', pack 's', hex($_));
	$byte_num++;
	if ($byte_num == 4)
	{
		printf("%d %d\n%d %d\n", $vals[3],  $vals[2], $vals[1], $vals[0]);
		$byte_num = 0;
		@vals = 0;
	}
}