我正在尝试将数字转换为网络,然后将结果放入向量中

std::vector<char> vctData;
u_long lnum = 145;
lnum = htonl(lnum);
//This line should put lnum at the beginning of vector in the first 4 bytes
vctData.insert(vctData.begin(), ???, ???);


该怎么办 ???

谢谢

最佳答案

std::vector<unsigned char> vctData;
u_long lnum = 145;
lnum = htonl(lnum);
vctData.insert(vctData.begin(), (unsigned char *) &lnum, (unsigned char *) (&lnum + 1));


这将占用lnum的各个字节,并将它们一个一地放入vctData中。

09-08 10:30