od -x test shows:
0000000 457f 464c 0102 0001
Now I want to use Perl to create such file.
open FH,'>','test_1';
#syswrite(FH,0x457f464c01020001); # not work
print FH 0x457f464c01020001; # not work
How to create binary file in Perl?
To create a binary file, one uses
open (my $fh, '>:raw', $qfn)
To place
45 7f 46 4c 01 02 00 01
in that file, one can use any of the following:
# Starting with a string of those bytes.
print $fh "\x45\x7f\x46\x4c\x01\x02\x00\x01";
# Starting with a hex representation of the file.
print $fh pack('H*', '457f464c01020001');
# Starting with the bytes.
print $fh map chr, 0x45, 0x7f, 0x46, 0x4c, 0x01, 0x02, 0x00, 0x01;
# Starting with the bytes.
print $fh pack('C*', 0x45, 0x7f, 0x46, 0x4c, 0x01, 0x02, 0x00, 0x01);
open(my $out, '>:raw', 'test_1.bin') or die "Unable to open: $!";
print $out pack('s<',255) ;
close($out);
also you can look at the perl pack functions here
binmode(FH); It's worth understanding what 's' represents (for OP): perldoc.perl.org/functions/pack.htmlprint FH "\x45\x7f\x46\x4c\x01\x02\x00\x01" is another way. See Quote and Quote-like Operators in perlop for more information about escape sequences available in strings. \x works like it does in C... except that Perl has an extended syntax that goes beyond \xff.
print 1;to print a ctrl-A.