64,682
社区成员
发帖
与我相关
我的任务
分享
for (int i = 0; i < in_len; i++) {
if (0 == (in_utf8_str[i] & 0x80))
//是一个ascii码
else
//不是是一个ascii码
}
bool ChangeCharCode::cvt_Ucs2_Utf8
(const uint8* in_ucs2_str,
uint8* out_utf8_str,
int in_len, /* len of in_ucs2_str in 16 bit mode */
int* out_len, /* len of out_utf8_str in 8 bit mode */
endina_type endina_tpy /* endina_type of in_ucs2_str */) {
uint16 ucs2_big_ending = 0;
int pos = 0;
if (out_len) *out_len = 0;
if (in_ucs2_str && out_utf8_str) {
for (int i = 0; i < in_len; i++) {
if (BIG_ENDINA == endina_tpy)
ucs2_big_ending = (in_ucs2_str[2 * i] << 8) | in_ucs2_str[2 * i + 1];
else
ucs2_big_ending = (in_ucs2_str[2 * i + 1] << 8) | in_ucs2_str[2 * i];
if (0x7F >= ucs2_big_ending)
out_utf8_str[pos++] = (uint8)ucs2_big_ending;
else if (0x7FF >= ucs2_big_ending && 0x80 <= ucs2_big_ending) {
out_utf8_str[pos++] = 0xC0 | (ucs2_big_ending >> 6);
out_utf8_str[pos++] = 0x80 | (ucs2_big_ending & 0x3F);
}
else if (0xFFFF >= ucs2_big_ending && 0x800 <= ucs2_big_ending) {
out_utf8_str[pos++] = 0xE0 |(ucs2_big_ending >> 12);
out_utf8_str[pos++] = 0x80 |((ucs2_big_ending >> 6) & 0x3F);
out_utf8_str[pos++] = 0x80 |(ucs2_big_ending & 0x3F);
}
}
if (out_len) *out_len = pos;
return true;
}
else
return false;
}
bool ChangeCharCode::cvt_Utf8_Ucs2
(const uint8* in_utf8_str,
uint8* out_ucs2_str,
int in_len, /* len of in_utf8_str in 8 bit mode */
int* out_len, /* len of out_ucs2_str in 16 bit mode */
endina_type endina_tpy/* endina_type of out_ucs2_str*/) {
uint16* ucs2_str = 0;
int pos = 0;
if (out_len) *out_len = 0;
if (in_utf8_str && out_ucs2_str) {
ucs2_str = (uint16*)out_ucs2_str;
for (int i = 0; i < in_len; i++) {
if (0 == (in_utf8_str[i] & 0x80))
ucs2_str[pos++] = BIG_ENDINA == endina_tpy ?
in_utf8_str[i] : (in_utf8_str[i] << 8);
else if (0xC0 == (in_utf8_str[i] & 0xE0)) {
if (BIG_ENDINA == endina_tpy) {
ucs2_str[pos] = in_utf8_str[i] >> 2 & 0x07;
ucs2_str[pos] = (ucs2_str[pos] << 8) |(in_utf8_str[i + 1] & 0x3F) | ((in_utf8_str[i] & 0x03) << 6);
}
else {
ucs2_str[pos] = in_utf8_str[i] >> 2 & 0x07;
ucs2_str[pos] |= ((in_utf8_str[i + 1] & 0x3F) | ((in_utf8_str[i] & 0x03) << 6)) << 8;
}
++pos;
++i;
}
else if (0xE0 == (in_utf8_str[i] & 0xF0)) {
if (BIG_ENDINA == endina_tpy) {
ucs2_str[pos] = ((in_utf8_str[i] & 0x0F) << 4) | (in_utf8_str[i + 1] >> 2 & 0x0F);
ucs2_str[pos] = (ucs2_str[pos] << 8) | ((in_utf8_str[i + 1] & 0x03) << 6 | (in_utf8_str[i + 2] & 0x3F));
}
else {
ucs2_str[pos] = ((in_utf8_str[i] & 0x0F) << 4) | (in_utf8_str[i + 1] >> 2 & 0x0F);
ucs2_str[pos] |= ((in_utf8_str[i + 1] & 0x03) << 6 | (in_utf8_str[i + 2] & 0x3F)) << 8;
}
++pos;
i += 2;
}
}
if (out_len) *out_len = pos;
return true;
}
else
return false;
}