utf-8编码转换为ASCII码的问题

xuhui_7810 2009-01-10 12:57:27
各位大侠,小弟最近在用C++开发一个可以访问wap的浏览器。这些页面如果有<?xml version="1.0" encoding="UTF-8"?>这个信息,则表明页面是采用的utf-8编码。为了能让它正常显示,我必须将其转换成ASCII.但本人对这些编码问题不是很清楚,这个项目又比较急,所以想麻烦各位大侠,能给我一个将utf-8转换为ASICC码的函数不?
...全文
1420 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
iislz 2010-08-19
  • 打赏
  • 举报
回复
怎么看不到
xuhui_7810 2009-01-10
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 yellowhwb 的回复:]
引用 5 楼 xuhui_7810 的回复:
我这个页面上有汉字,有所有可能有的字符。现在我需要的是从utf8到ASCII的转换。那个到Ucs2的我已经有了。不过还是谢谢你哈,呵呵

如果只要转换所有字符中的ascii码的话,只要一句话就可以了

C/C++ code
for (int i = 0; i < in_len; i++) {
if (0 == (in_utf8_str[i] & 0x80))
//是一个ascii码
else
//不是是一个ascii码
}
[/Quote]

这位兄弟你没有明白我的意思。我的意思是,把整个页面,从utf-8还原成ascii码。整个页码虽然有汉字,有英文等等,但是它们都是以utf-8的形式编码的,现在我要将它解成ascii码的。 以前没做过这东东,还真挺麻烦的
yellowhwb 2009-01-10
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 xuhui_7810 的回复:]
我这个页面上有汉字,有所有可能有的字符。现在我需要的是从utf8到ASCII的转换。那个到Ucs2的我已经有了。不过还是谢谢你哈,呵呵
[/Quote]
如果只要转换所有字符中的ascii码的话,只要一句话就可以了

for (int i = 0; i < in_len; i++) {
if (0 == (in_utf8_str[i] & 0x80))
//是一个ascii码
else
//不是是一个ascii码
}
xuhui_7810 2009-01-10
  • 打赏
  • 举报
回复
我这个页面上有汉字,有所有可能有的字符。现在我需要的是从utf8到ASCII的转换。那个到Ucs2的我已经有了。不过还是谢谢你哈,呵呵
yellowhwb 2009-01-10
  • 打赏
  • 举报
回复
两个函数分别是uft8转ucs2和ucs2转utf8!
yellowhwb 2009-01-10
  • 打赏
  • 举报
回复
我写的utf8转ucs2的函数,lz参考一下

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;
}
yellowhwb 2009-01-10
  • 打赏
  • 举报
回复
参考《UTF8编码的初步认识》http://hi.baidu.com/geortin/blog/item/9965314536c4243c879473d3.html
yellowhwb 2009-01-10
  • 打赏
  • 举报
回复
对于ascii编码的字符,utf8的数据和ascii的数据是完全一样的!

64,682

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧