问题描述
如何编写执行 -N 选项在Perl?
How do I write C++ code that does what the pack -N
option does in Perl?
我想将整数变量转换为二进制形式,使 -N选项可以返回整数变量。
I want to convert an integer variable to some binary form such that the unpack -N option on it gives back the integer variable.
我的整数变量名是时间戳。
My integer variable name is timestamp.
我发现它与htonl相关,但仍然htonl(timestamp)不给出二进制形式。
I found that it is related to htonl, but still htonl(timestamp) does not give the binary form.
推荐答案
我写了一个库, libpack
,类似Perl的 pack
函数。它是一个C库,所以它也可以很好地从C ++使用:
I wrote a library, libpack
, similar to Perl's pack
function. It's a C library so it would be quite usable from C++ as well:
FILE *f;
fpack(f, "u32> u32>", value_a, value_b);
A u32>
指定无符号32位整数的大尾数格式;即相当于Perl的 N
格式为 pack()
。
A u32 >
specifies an unsigned 32-bit integer in big-endian format; i.e. equivalent to Perl's N
format to pack()
.
这篇关于C ++相当于'pack'在Perl中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!