符号整数网络和主机的转换 [英] Signed Integer Network and Host Conversion

查看:233
本文介绍了符号整数网络和主机的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从主机字节顺序到网络字节顺序,反之亦然转换 int32_t 。我知道关于 htonl()函数及其变种,但是这需要无符号整数。有没有一个标准库函数,可以做同样的符号整数或做我要实现它自己?如果我要实现它自己,我应该怎么办呢?

I would like to convert a int32_t from host byte order to network byte order and vice versa. I know about the htonl() function and its variants, but this takes unsigned integers. Is there a standard library function which can do the same with signed integers or do I have to implement it myself? And if I have to implement it myself, how should I do it?

我希望找到一个例程将在Linux和Mac OS X上运行。

I'm looking to find a routine that will work on Linux and Mac OS X.

推荐答案

没关系。 htonl关注的是字节,而不是用数字的算术值。使用reinter pret_cast再次改变为无符号和后面的电话号码,如果你不得不这样做。

It does not matter. htonl is concerned with bytes, not with arithmetical value of the number. Use reinterpret_cast to change the number to unsigned and back again, if you have to.

这篇关于符号整数网络和主机的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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