Question : How to convert a binary file to Hex string? (Perl)

Experts,

how do I convert a binary file to a hex string in Perl?

not-so-working example:

      open(BS_FILE, $bs_file_bin)  or die "can't open $bs_file_bin: $!";
      
      binmode(BS_FILE);
      
      @lines = <BS_FILE>;
      for $line ( @lines ) {
            chomp $line;
         my $int = unpack("N", pack("B32", substr("0" x 32 . $line, -32)));
            my $hex = sprintf("%x", $int );

            print STDOUT "$hex\n";

      }

Answer : How to convert a binary file to Hex string? (Perl)

I think the issue is that you are mixing <BS_FILE> with binmode.
Try this... it seems to work for me.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
use strict;

my $bs_file_bin = "/tmp/bf";

open(BS_FILE, $bs_file_bin)  or die "can't open $bs_file_bin: $!";
binmode(BS_FILE);

my $data;
while( read(BS_FILE, $data, 4) ) {
   my $int = unpack("N", $data);
   my $hex = sprintf("%08X", $int );
   print STDOUT "$hex\n";
}
Random Solutions  
 
programming4us programming4us