将整数转换为无符号长整数(Convert Integer to Unsigned Long)
尝试将函数htonl()用于向服务器发送init消息的程序,以及整数值,例如5.但是, htonl()需要以下uint32_t整数hostlong
如何将5转换为无符号整数?
Trying to use the function htonl() for a program that sends an init message to a server, along with an integer value, say 5. However, htonl() needs the following uint32_t integer hostlong
How can I convert 5 to an unsigned integer?
原文:https://stackoverflow.com/questions/30467260
更新时间:2020-02-25 15:27
最满意答案
htonl函数在 。 假设你有一个适合该标题的#include :
#include `
声明
uint32_t htonl(uint32_t hostlong);
将是可见的,因此编译器知道期望的参数类型和结果类型。
如果要将值5传递给htonl函数,只需传递它:
uint32_t result = htonl(5);
常量5的类型为int 。 编译器将生成从int到uint32_t的隐式转换。 (转换很可能实际上不需要做任何事情。)
如果值5存储在int对象中,则它是相同的:
int n = 5;
uint32_t r