#!/usr/bin/perl -w use strict; use warnings; # Usage: STATUS = CreateHexFile(FILENAME, [STRINGS...]) sub CreateHexFile { defined $_[0] or return 0; my $F = shift; # Catch illegal characters in the file name $F =~ m/[\x00-\x1F*?|<>]{1}/ and return 0; # Join multiple arguments into one string my $hexstr = join('', map(defined $_ ? $_ : '', @_)); # Remove everything except hexadecimal digits $hexstr =~ tr|0-9A-Fa-f||cd; local *FILE; open(FILE, ">$F") or return 0; # Create binary file binmode FILE; # Convert hex string to a binary string and write it to the file. # If there is an odd number of hexadecimal digits in $hexstr, then # a zero digit is automatically added to the end at conversion. print FILE pack('H*', $hexstr); close FILE; return 1; } #################################################################### # Create a list of hexadecimal numbers from 00 to FF: my @numbers = map(sprintf('%.2X', $_), (0..255)); CreateHexFile('ASCII.BIN', @numbers);