Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::vector hex values

Tags:

c++

hex

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?

like image 685
elisa Avatar asked Jan 13 '11 10:01

elisa


1 Answers

(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.

like image 71
Lightness Races in Orbit Avatar answered Sep 30 '22 15:09

Lightness Races in Orbit