How to Perl how to convert a binary string to an integer?

I have a four byte string read from a binary file that should represent an integer. How can I stop an integer?

Example:

my $s = '\xa8e2~'; my $i = stoi($s); printf "%X", $i; #gives "0x7e3265a8" 

The solution in C is simple:

 fread(&i,4,1,fp); 
+4
source share
2 answers

$i = unpack("s", $s) may work, but it depends on the subscription order / unsigned and byte, so you will probably find yourself here: http://perldoc.perl.org/perlpacktut.html#Integers

+6
source

Look at the unpack function

+3
source

Source: https://habr.com/ru/post/1332276/


All Articles