Ruby如何将32位整数转换为网络字节顺序? [英] Ruby how to convert 32 bit integer to network byte order?

查看:117
本文介绍了Ruby如何将32位整数转换为网络字节顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

如何将32位整数转换为网络字节顺序?

我已经问过 如何将32位整数转换为网络字节顺序?,但我还是不明白这个概念。

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

我需要写出长度

例如,如果以字节为单位的数据大小为1024,则需要在Ruby中将其表示为网络字节顺序。

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")

但是当我尝试

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

正确的方法是什么?

推荐答案

在Ruby中将32位整数转换为网络字节顺序的正确方法是

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

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

这篇关于Ruby如何将32位整数转换为网络字节顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆