Bin2Hex.pl - Converting Binary Data to Hex Numbers

This section provides a tutorial example on how to convert binary data file to hex numbers - Bin2Hex.pl.

As another example, I wrote Bin2Hex.pl to convert binary data to hex numbers:

```#- Bin2Hex.pl
#
(\$in, \$out) = @ARGV;
die "Missing input file name.\n" unless \$in;
die "Missing output file name.\n" unless \$out;
\$byteCount = 0;
open(IN, "< \$in");
binmode(IN);
open(OUT, "> \$out");
\$n = length(\$b);
\$byteCount += \$n;
\$s = 2*\$n;
print (OUT unpack("H\$s", \$b), "\n");
}
close(IN);
close(OUT);
print "Number of bytes converted = \$byteCount\n";
exit;
```

Of course, I had to write Hex2Bin.pl to convert hex numbers back to binary data:

```#- Hex2Bin.pl
#
(\$in, \$out) = @ARGV;
die "Missing input file name.\n" unless \$in;
die "Missing output file name.\n" unless \$out;
\$byteCount = 0;
open(IN, "< \$in");
open(OUT, "> \$out");
binmode(OUT);
while (<IN>) {
chop;
\$s = length(\$_);
\$byteCount += \$s/2;
print (OUT pack("H\$s", \$_));
}
close(IN);
close(OUT);
print "Number of bytes converted = \$byteCount\n";
exit;
```

To test this program, I used the following commands:

```C:\herong>copy c:\windows\system32\find.exe find.exe
1 file(s) copied.

C:\herong>Bin2Hex.pl find.exe find.hex
Number of bytes converted = 17408

C:\herong>Hex2Bin.pl find.hex find_copy.exe
Number of bytes converted = 17408

C:\herong>dir find*.*
17,408 find.exe
35,904 find.hex
17,408 find_copy.exe

C:\herong>find_copy.exe /?
Searches for a text string in a file or files.

FIND [/V] [/C] [/N] [/I] [/OFF[LINE]] "string" ...
...
```