in reply to BIN to HEX
Secondly reading the whole file into memory might be problematic - particularly if you may have to deal with a very large file. It's better to read the file in fixed size chunks - then you can put an upper bound on how much memory your program uses which doesn't depend on the size of the files you feed it.
Finally unpack() is useful when working with non-textual data. Here it's used to turn a 8192 (or less) character string into an 8192 element array of byte values.
#!/usr/bin/perl -w use strict; $| = 1; my $MAXVALUE = 0x30; my $file = shift or die "Please name a file\n"; open my $fh, '<', $file or die "Can't open $file ($!)\n"; binmode $fh; my $buffer; CHUNK: for (;;) { # Read as much as 8k, less if at end of file my $rc = sysread $fh, $buffer, 8192; die "Read error\n" unless defined $rc; # No more bytes? last CHUNK if $rc == 0; for my $byte (unpack('C*', $buffer)) { if ($byte > $MAXVALUE) { printf("%02x > %02x\n", $byte, $MAXVALUE); last CHUNK; } } } close $fh;
|
|---|