uint8_t需要uint16_t来存储来自stringstream的值

时间:2017-04-30 10:45:51

标签: c++11 casting stringstream uint8t

从argv读取IP地址迫使我将其转换为uint32_t

此功能将执行此操作:

uint32_t convert_string_ip_to_uint32_t (const string & string_ip)
{
    stringstream s(string_ip) ;
    uint16_t a,b,c,d; //to store the 4 ints
    char ch; //to temporarily store the '.'
    s >> a >> ch >> b >> ch >> c >> ch >> d ;
    std::cout << a << "  " << b << "  " << c << "  "<< d << std::flush;;
    return ((uint32_t) a << 24 | b << 16 | c << 8 | d);
}

但是当我将a,b,c和d的数据类型更改为uint8_t时,结果将是rubish。为什么? 例如string_ip ='192.168.1.10'

0 个答案:

没有答案