htonl()与__builtin_bswap32() [英] htonl() vs __builtin_bswap32()

查看:5267
本文介绍了htonl()与__builtin_bswap32()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

__ builtin_bswap32()用于反向字节(它用于LITTEL /大端问题(从GCC))。

__builtin_bswap32() is used to reverse bytes (it's used for littel/big endian issues (from gcc)).

htonl()用于反向太字节(转换从主机到网络)。

htonl() is used to reverse bytes too (conversion from host to network).

我查了一下两者的功能和它们返回相同的结果。

I checked both functions and they returns the same result.

是否有一些一谁可以证实,这两个函数做同样的事情? (标准refences是AP preciated)

Are there some one who can confirm that both functions do the same thing? (standard refences are appreciated)

推荐答案

只要看看源$ C ​​$ C:(从2.18巧舌如簧的例子)

Just look at source code : (example from glib 2.18)

#undef htonl
#undef ntohl

uint32_t
htonl (x)
uint32_t x;
{
    #if BYTE_ORDER == BIG_ENDIAN
       return x;
    #elif BYTE_ORDER == LITTLE_ENDIAN
       return __bswap_32 (x);
    #else
       # error "What kind of system is this?"
    #endif
}
weak_alias (htonl, ntohl)

的#define __bswap_32(X)((无符号整数)__ builtin_bswap32(X))

在这里来源:<一个href=\"http://fossies.org/dox/glibc-2.18/htonl_8c_source.html\">http://fossies.org/dox/glibc-2.18/htonl_8c_source.html

正如你所看到的, htonl 只能叫 __ builtin_bswap32 对小端的机器。

As you can see, htonl only call __builtin_bswap32 on little-endian machines.

这篇关于htonl()与__builtin_bswap32()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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