How do I convert between big-endian and little-endian values in C++?

前端 未结 30 2547
难免孤独
难免孤独 2020-11-21 23:18

How do I convert between big-endian and little-endian values in C++?

EDIT: For clarity, I have to translate binary data (double-precision floating point values and 3

30条回答
  •  独厮守ぢ
    2020-11-22 00:10

    If you are doing this for purposes of network/host compatability you should use:

    ntohl() //Network to Host byte order (Long)
    htonl() //Host to Network byte order (Long)
    
    ntohs() //Network to Host byte order (Short)
    htons() //Host to Network byte order (Short)
    

    If you are doing this for some other reason one of the byte_swap solutions presented here would work just fine.

提交回复
热议问题