I am writing a C++ program where I Need to convert some string output which is basically in HEX format. This output string starts with FFD8 and Ends with FFD9 which is basically a JPEG Image.
Now, I want to get the JPEG file from that string output but I don't want to save that string output in a text file and open it in ios::binary mode and then covert it to a JPEG file.
std::string output; //which is FFD8..........FFD9
//******some code*******???
ofstream imageFile;
imageFile.open('Image.jpg');
imageFile<< output;
How I can do that without saving my string output in a file?
Thanks in advance!
My assumption is that you have a string representing hex, and you want to convert that to the byte equivalent.
byte hexCharToByte(const char h){
if(isdigit(h))
return h - '0';
else
return toupper(h) - 'A' + 10;
}
This is some code I wrote a while back in C that takes a char and converts it to a byte. You can adapt it however you need.
How it works:
std::string output; //which is FFD8..........FFD9
int main()
{
std::ofstream thum("name.jpg", std::ios_base::binary | std::ios_base::out);
char buf[3];
buf[2] = 0;
std::stringstream in(output);
in.flags(std::ios_base::hex);
while (in)
{
in >> buf[0] >> buf[1];
long val = strtol(buf, nullptr, 16);
thum << static_cast<unsigned char>(val & 0xFF);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.