问题
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