Ruby how to convert 32 bit integer to network byte order? [duplicate]

别来无恙 提交于 2019-12-11 08:09:35

问题


Possible Duplicate:
How to convert 32 bit integer to network byte order?

I already asked "How to convert 32 bit integer to network byte order?" but still I don't understand the concept.

I need to write the length of the data to socket in network byte order.

For example if the size of the data in bytes is 1024 then I need to express this as network byte order in Ruby.

As per the answer for my earlier question it will be:

[1,0,2,4].pack("NNNN")

But when I try this it also works:

[10,24].pack("NN")
[4,2,0,1].pack("NNNN")

What is the correct way to do it?


回答1:


Correct way to convert 32 bit integer to network byte order in Ruby is

#[integer].pack("N")
[1024].pack("N")


来源:https://stackoverflow.com/questions/12706052/ruby-how-to-convert-32-bit-integer-to-network-byte-order

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!