assigning binary to vector C++
I am trying to convert a number to network and then to put the result in a vector in
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(), ???, ???);
what to do ???
Thanks
std::vector<unsigned char> vctData;
u_long lnum = 145;
lnum = htonl(lnum);
vctData.insert(vctData.begin(), (unsigned char *) &lnum, (unsigned char *) (&lnum + 1));
This takes the individual bytes of lnum and puts them one by one into vctData.
EDIT: After knowing, you need to add lnum
to a vector of char, then do this:
std::vector<char> vctData; //note: mention the type argument!
u_long lnum = 145;
lnum = htonl(lnum);
//inserts in the beginning!
vctData.insert(vctData.begin(),(char*)&lnum, (char*)&lum + sizeof(u_long));
Try this. Let me know if it works for you.
精彩评论