Home: Perl Programming Help: Beginner:
sysread and Little Endian

New User

Oct 4, 2013, 1:34 AM

Views: 2478
sysread and Little Endian

Hi all,
my file, in HEX, is like this:

hexdump -C mytest.pcap -n 16 
00000018 63 1e 0f 4f b5 a2 0e 00 2a 05 00 00 2a 05 00 00 |c..O....*...*...|

I need to sysread the last 4 bytes (2a 05 00 00) which indicates the total file length. Beware that the number is written in Little Endian and must be read as 0x0000052a --> 1322 Bytes

sysread($global_fh, $buffer, 12) > 0 || die "couldn't read byte from $filename: $!"; 
sysread($global_fh, $buffer, 4) > 0 || die "couldn't read byte from $filename: $!";
$buf = unpack('H*',$buffer);
print "-". $buf ."-\n";


How to convert now in decimal?
Help is appreciated!


(This post was edited by ric79 on Oct 6, 2013, 11:37 PM)
Attachments: mytest.pcap (1.33 KB)

Veteran / Moderator

Oct 4, 2013, 3:16 PM

Views: 2459
Re: [ric79] sysread and Little Endian

I would probably first try to use the read and seek functions, rather than sysread, to read the required bytes.

This is a basic syntax example I gave on this forum a few weeks ago, you can adapt it to your needs:

  DB<1> $c = "The quick brown fox jumps over the lazy dog."  

DB<2> open $fh, "<", \$c or die "could not open $c $!"; # opens a reference to the $c variable as a filehandler

DB<3> seek $fh, 10, "00"; # positions the filehandler on the 10th byte

DB<4> read $fh, $out, 10; # reads 10 bytes from position 10

DB<5> print $out;
brown fox

For the Little Endian/Big Endian thing, hmm, we would probably need to know more about your machine architecture (64 bits, 48 bits, 64 bits, more ?), I am not even sure you need to care about it if you use the read function (last time I had to deal with such problems was at least 6 years ago, I don't remember well enough what I had to do, except that I had to make a bit of experiment to get it right, but it was not so complicated to get it right once I understood the format). One possible clue: the h and H templates of the pack and unpack function might be what you are looking for.

I might have a quick look at it next Monday when I am back to work, where I have access to a Little Endian platform.

Chris Charley

Oct 4, 2013, 7:34 PM

Views: 2455
Re: [ric79] sysread and Little Endian

I'm not sure this is what you want, but it seems to give the correct number, 1322.

use strict;
use warnings;

$_ = '2a050000';

print unpack "N", pack "V", hex $_;

New User

Oct 6, 2013, 11:29 PM

Views: 2428
Re: [Laurent_R] sysread and Little Endian

Thanks, I switched to your syntax but now I'm still on the same problem

$out contains, i think, 0x2a 05 00 00.

How can I do the convertion to decimal? I attached in the first question the binary file. I'm trying to decode the bytes in [20-23] range.

Thanks to all!

(This post was edited by ric79 on Oct 6, 2013, 11:38 PM)


Oct 8, 2013, 7:36 AM

Views: 2400
Re: [ric79] sysread and Little Endian

I had to change the number of bytes in the first sysread to read your sample file. This code runs on perl v5.16.1 under windows xp.

open my $global_fh, '<', 'mytest.pcap', or die "cannot open $!"; 
binmode $global_fh;
sysread( $global_fh, $buffer, 0x20 ) > 0
|| die "couldn't read byte from $filename: $!";
sysread( $global_fh, $buffer, 4 ) > 0
|| die "couldn't read byte from $filename: $!";

$buf = unpack( 'H8', $buffer );
print "-" . $buf . "-\n";
$buf = unpack( 'V', $buffer );
print "-" . $buf . "-\n";



Update: Corrected first sysread. Note that second syread reads bytes 0x20 thru 0x23.
Good Luck,

(This post was edited by BillKSmith on Oct 8, 2013, 10:12 AM)

New User

Oct 8, 2013, 10:34 PM

Views: 2384
Re: [BillKSmith] sysread and Little Endian

Thanks to all!
It work