开发者

Printing integers in 32-bit fixed length binary

开发者 https://www.devze.com 2023-02-03 23:53 出处:网络
I am using Perl to translate an integer provided by the user to binary form. For example, if the user开发者_C百科 input is \"3735928559\" (0xdeadbeef in hexadecimal representation), the program should

I am using Perl to translate an integer provided by the user to binary form. For example, if the user开发者_C百科 input is "3735928559" (0xdeadbeef in hexadecimal representation), the program should output four chars respectively encoded in \xde, \xad, \xbe, \xef, instead of "deadbeef".

I don't want to use external modules, then how can I do this? Thank you.


If I understand right (you want the four bytes 0xde, 0xad, 0xbe, and 0xef), try the following:

print pack("N", $input);


Your question isn't very clear, but I think you're looking for pack:

my $input = '3735928559';
print pack('N', $input);
0

精彩评论

暂无评论...
验证码 换一张
取 消