0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
三、附上网上找的UTF-16转UTF-8的代码(由于本人的应用只涉及两个字节的UTF-16编码,故以下代码不大于0xFFFF的字符转换)
static void UTF16ToUTF8(uint16_t* pUTF16, int32_t UTF16Len, UTF8* pUTF8, int32_t UTF8Len) { uint16_t* pData = NULL; int32_t utf8Offset = 0; for (int32_t i = 0; i < UTF16Len; i++) { pData = &pUTF16[i]; if (*pData <= 0x007F && (utf8Offset + 1) <= UTF8Len) { pUTF8[utf8Offset++] = *pData; } else if ((*pData >= 0x0080 && *pData <= 0x07ff) && (utf8Offset + 2) <= UTF8Len) { pUTF8[utf8Offset++] = (*pData >> 6) | 0XC0; pUTF8[utf8Offset++] = (*pData & 0x3F) | 0x80; } else if (*pData >= 0x0800 && utf8Offset + 3 <= UTF8Len) { pUTF8[utf8Offset++] = (*pData >> 12) | 0xE0; pUTF8[utf8Offset++] = ((*pData >> 6) & 0x3F) | 0x80; pUTF8[utf8Offset++] = (*pData & 0x3F) | 0x80; } else { break; } } } static void UnicodeCharToUtf8(const char *pUncodeChar, int32_t UncodeCharLength, UTF8* pUTF8, int32_t UTF8Len) { if (NULL == pUncodeChar || UncodeCharLength <= 0) { return; } int32_t wcharLen = UncodeCharLength / 4; wchar16_t* wStr = new wchar16_t[wcharLen + 1]; memset(wStr, 0x00, sizeof(wchar16_t) * (wcharLen + 1)); for (int32_t j = 0; j < wcharLen; j++) { char temp[5] = { 0 }; memcpy(temp, &pUncodeChar[j * 4], 4); wStr[j] = strtol(temp, NULL, 16); } UTF16ToUTF8(wStr, wcharLen, pUTF8, UTF8Len); }
即将Environment设置成与你要插入的中文字符的编码格式相同,则插入的内容就会在DB中显示成中文。
若不设置Environment,则occi会默认使用系统环境变量NLS_LANG的值来作为编码格式,所以也可以通过设置环境变量来设置Environment的中文编码格式
export NLS_LANG=american_america.AL32UTF8