vendredi 28 juin 2019

How to convert unsigned short into unsigned char and store the value in string

I'm trying to convert unsigned short value to unsigned char type and store the result in the string. Then again converting the string back to unsigned char to get back the original unsigned short values.I got struct around this conversion.

I have converted unsigned short values to string. How do I get back unsigned short value using unsigned char array.Please help me out to solve this

unsigned short a=1234;
 unsigned short b=5678;
 std::string str="";
 str+=std::to_string((unsigned char)a & 0xFF);
 str+=std::to_string((unsigned char)(a>>8)& 0xFF);
 str+=std::to_string((unsigned char)b & 0xFF);
 str+=std::to_string((unsigned char)(b>>8)& 0xFF);
 cout<<"String Value: "<<str<<endl;

expected output to be "12345678" where a=1234 b=5678;

Aucun commentaire:

Enregistrer un commentaire