On 8 April 2010 06:30, Raymond Wan <r....@aist.go.jp> wrote: > > Hi all, > > I would like to read in a binary file and extract the 4-byte ints from it. > Under Linux, something like "od -t uI". I got it working as follows: > > > my $buffer = <STDIN>; > my @buffer = split //, $buffer; > for (my $i = 0; $i < length ($buffer); $i += 4) { > print unpack ('I', $buffer[$i].$buffer[$i + 1].$buffer[$i + 2].$buffer[$i + > 3]), "\n"; > }
Are you sure you want to read using the <> line input operator? Consider the file (as a hexdump): 00000000 000a0000 There are 8 bytes, corresponding to 2 4-byte ints. The two 4-byte ints have values 0 and 10*256*256 = 655360 (assuming big endian). But if 0a happens to be the value of a newline char (as it is under ascii and utf-8) and you read line-by-line you get two lines: "\0\0\0\0\0" and "\0\0" which your program will interpret as three ints of values (0,0,0), and not 2 ints with values (0,655360). Use 'read' instead (perldoc -f read) to read binary data. Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/