I have string (key_str) of size 32 bytes. I want to store each bytes in uint8_t array element key[32]. I tried the following:
string key_str = "00001000200030004000500060007000";
uint32_t key[32] ;
uint8_t* k = reinterpret_cast <uint8_t*>(&key_str[0]);
for(int j = 0; j < 32; j++)
{
key[j]= *k;
k++;
cout<<bitset<8>(key[j])<<endl;
}
but the MSB 4 bits of the output is always 0011 because of representation of characters (0,1,...) so how can I convert it to integer?
Output sample: 00110000 .. 00110001 .. 00110010 ..
std::min(key_str.length(), 32). Instead of literal 32, prefer usingsizeof(key)/sizeof(*key), you can then change array size without danger of forgetting to adjust the other places. If you usestd::arrayinstead of raw array, you can use itssize()member instead of the ugly, but otherwise necessary division by size of first element (be aware ofsizeofalways giving size in bytes!)