十六進制轉(zhuǎn)ascii
std::string hexToAscii(const std::string& hexString) {
std::string asciiString;
for (size_t i = 0; i < hexString.length(); i += 2) {
if(hexString[i]=='\\')
{
continue;
}
// 從兩個字符表示的十六進制數(shù)轉(zhuǎn)換為整數(shù)
int decimal = std::stoi(hexString.substr(i, 2), nullptr, 16);
// 將整數(shù)轉(zhuǎn)換為對應(yīng)的 ASCII 字符
asciiString += static_cast<char>(decimal);
}
return asciiString;
}
unicode轉(zhuǎn)UTF8
std::string unicodeToUTF8(const std::string& unicodeStr)
{
std::string utf8Str;
for (std::size_t i = 0; i < unicodeStr.size(); i += 6)
{
std::string unicodeCode = unicodeStr.substr(i + 2, 4);
unsigned long unicodeValue = std::stoul(unicodeCode, nullptr, 16);
if (unicodeValue <= 0x007F)
{
utf8Str += static_cast<char>(unicodeValue);
}
else if (unicodeValue <= 0x07FF) {
utf8Str += static_cast<char>(0xC0 | ((unicodeValue >> 6) & 0x1F));
utf8Str += static_cast<char>(0x80 | (unicodeValue & 0x3F));
}
else {
utf8Str += static_cast<char>(0xE0 | ((unicodeValue >> 12) & 0x0F));
utf8Str += static_cast<char>(0x80 | ((unicodeValue >> 6) & 0x3F));
utf8Str += static_cast<char>(0x80 | (unicodeValue & 0x3F));
}
}
return utf8Str;
}
UTF8轉(zhuǎn)unicode
std::string to_hex_string(unsigned long value, int width)
{
std::stringstream ss;
ss << std::hex << std::uppercase << std::setw(width) << std::setfill('0') << value;
std::string hexStr = "\\u" + ss.str();
// 轉(zhuǎn)換為小寫形式
for (char& c : hexStr)
{
c = std::tolower(c);
}
return hexStr;
}
std::string utf8ToUnicode(const std::string& utf8Str)
{
std::string unicodeStr;
std::size_t i = 0;
while (i < utf8Str.size())
{
unsigned char byte1 = static_cast<unsigned char>(utf8Str[i]);
if ((byte1 & 0x80) == 0) // 1個字節(jié)
{
unicodeStr += utf8Str[i];
i++;
}
else if ((byte1 & 0xE0) == 0xC0) // 2個字節(jié)
{
unsigned char byte2 = static_cast<unsigned char>(utf8Str[i + 1]);
unsigned long unicodeValue = ((byte1 & 0x1F) << 6) | (byte2 & 0x3F);
unicodeStr += to_hex_string(unicodeValue, 4); // 轉(zhuǎn)換為Unicode編碼
i += 2;
}
else if ((byte1 & 0xF0) == 0xE0) // 3個字節(jié)
{
unsigned char byte2 = static_cast<unsigned char>(utf8Str[i + 1]);
unsigned char byte3 = static_cast<unsigned char>(utf8Str[i + 2]);
unsigned long unicodeValue = ((byte1 & 0x0F) << 12) | ((byte2 & 0x3F) << 6) | (byte3 & 0x3F);
unicodeStr += to_hex_string(unicodeValue, 4); // 轉(zhuǎn)換為Unicode編碼
i += 3;
}
else
{
// 不支持的UTF-8編碼格式
// 可以根據(jù)需要進行處理或拋出異常
break;
}
}
return unicodeStr;
}