Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如何将 32 位整数转换为网络字节顺序。
这样做的正确方法是什么?
[1024].pack("N")
或者
[1,0,2,4].pack("N")
谢谢
首先,查看每个的输出:
>> [1024].pack("N") => "\000\000\004\000" >> [1,0,2,4].pack("N") => "\000\000\000\001"
请注意第二个缺少的内容:
>> [1,0,2,4].pack("NNNN") => "\000\000\000\001\000\000\000\000\000\000\000\002\000\000\000\004"