简体   繁体   English

在Perl中,如何将二进制字符串转换为整数?

[英]In Perl, how can I convert a binary string to an integer?

I have a four-byte string read from a binary file, which is supposed to represent an integer. 我有一个从二进制文件读取的四字节字符串,它应该表示一个整数。 How do I dervive the integer? 我如何设置整数?

Example: 例:

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

The solution in C is simply: C中的解决方案很简单:

fread(&i,4,1,fp);

$i = unpack("s", $s)可能有效,但它取决于有符号/无符号和字节顺序,所以你可能会在这里结束: http//perldoc.perl.org/perlpacktut.html#Integers

看看unpack功能

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM