开发者

std::vector hex values

How do you initialize an std::vector with hex values? The follow throws an error:

std::vector<unsigned char> vect ("oc","d4","30");

If I have a string value that containes a base64 code like: "DNQwSinfOUSSWd+U04r23A==" ....how can I put it in a std::vectorv?

std::string = "DNQwSinfOUSSWd+U04r23A==";

I first want to decode it to hexa values. After that to put it in a vector. Can someone please tell me how to decode the 开发者_运维问答string value that contains base64 encoder into a hexa?


(You forgot to give your std::vector an element type. I'll assume unsigned char.)

In C++0x you will be able to write:

std::vector<unsigned char> v{ 0x0C, 0xD4, 0x30 };

In C++03 you have to write:

std::vector<unsigned char> v;
v.push_back(0x0C);
v.push_back(0xD4);
v.push_back(0x30);

Or, if you don't mind using up the space:

unsigned char values[] = { 0x0C, 0xD4, 0x30 };
std::vector<unsigned char> v(values, values+3);

You could also look at boost.assign.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜