htonl() vs __builtin_bswap32()

前端 未结 3 1951
我寻月下人不归
我寻月下人不归 2021-02-10 04:16

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

htonl() is used to reverse bytes too (conver

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-10 05:01

    I disagree with your statement that htonl() is used to reverse byte order. It is used to convert from host to network byte order. Depending on the byte order of the host, that may, or may not, result in byte order being reversed.

    I suggest that you use the function that expresses the correct semantic intent:

    • If you wish to convert from host to network byte order, use htonl().
    • If you wish to reverse byte order, use __builtin_bswap32().

提交回复
热议问题