c++ 十六进制数组转字符串转ascii

#include 
#include 
#include 

using namespace std;

int main()
{
    cout << "Hello World!" << endl;

    unsigned char mm[5] = { 0xff, 0xd8, 0xff, 0xe1, 0x95};

    char str_data[1024] = {0};
    for(int i = 0; i < 5;i++) {
        sprintf(str_data + i * 2, "%02x", mm[i] & 0xFF);
    }
    printf("%s\n", str_data);

    string str_ascii;
    for(int i=0;i<5*2;i++) {
        char hext[1024] = {0};
        sprintf(hext, "%02x", str_data[i]);
        str_ascii += hext;
    }
    cout<

运行结果:c++ 十六进制数组转字符串转ascii_第1张图片

十六进制转字符串的其他方法函数:

string strToHex(vector img_encode)
{
    stringstream ss;
    const string hex = "0123456789ABCDEF";
    for (string::size_type i = 0; i < img_encode.size(); ++i)
        ss << hex[(unsigned char)img_encode[i] >> 4] << hex[(unsigned char)img_encode[i] & 0xf];

    return ss.str();
}

你可能感兴趣的:(C/C++,c++,开发语言)