例如 “中国”这两个字在 baidu WAP URL 中的编码是 %E4%B8%AD%E5%9B%BD 为 utf-8 编码后转成 16 进制
对于 GB2312转换成 UTF-8格式 的思路是 先把 GB2312转换成 UCS2
再通过 UCS2转换成 UTF-8,但是转换后的 UTF-8,并没有添加 % ,要手动添加 %,
通过sprintf函数进行,组合 URL ,其中 % 以"%c"拼接, 其它以 utf-8 编码要 转成 16 进制
sprintf(url, "%s%c%x", url,UTF8_code[i], UTF8_code[i + 1]);
最后,%E4%B8%AD%E5%9B%BD 显示出来, 一切 OK ,
我错就错在,把 % 也给转换成 16进制了,
(VRE没有 GB2312直接转换成UTF-8的接口)
/************************************************************************/
/* 代开wap网站,from vre */
/************************************************************************/
void openwap_vre(){
// VMCHAR url[500]={0};
// VMWCHAR wurl[500]={0};
// VMCHAR vale[20];
//strcpy(url,"http://wap.baidu.com/?from=vogins");
//strcpy(url,"http://wap.baidu.com/news?tn=wap&ct=2&word=todaynews&pi=0&pn=10&from=tradeid&st=1044D6");
//strcpy(url,"http://wap.baidu.com/s?vit=union&from=vogind&word=ok");
//strcpy(url,"http://wap.baidu.com/s?vit=union&from=vogins&st=103041");
//vm_gb2312_to_ucs2(url,500,"http://wap.baidu.com/tq?area=上海&index=1&vit=index&from=vogins");
//vm_url_encode_gb2312(url,500,"http://wap.baidu.com/tq?area=&index=1&vit=index&from=vogins");
//vm_open_wap_url(vm_ucs2_string(url));
//strcpy(url,"http://wap.baidu.com/tq?&ssid=0&from=0&area=");
//http://wap.baidu.com/tq?&ssid=0&from=0&area=%E4%B8%8A%E6%B5%B7&vit=tj&uid=tq_1256641735_518&bd_page_type=0
//vm_url_encode_gb2312(vale,200,"香港");
VMCHAR url[500]={0};
VMWCHAR wurl[500]={0};
VMCHAR url2[500]={0};
VMUINT8 UTF8_code[255]={0};
VMUINT8 *p_UTF8=NULL;
VMUINT16 ucs2_code[255]={0};
VMINT i=0;
VMINT j=0;
VMINT len_UTF8=0;//UFT8数据的长度
vm_gb2312_to_ucs2((VMINT16*)ucs2_code, 255, "上海");//先转换成 UCS2格式
UCS2toUTF8Code(ucs2_code,UTF8_code); //UCS2格式转换成 UTF-8格式
p_UTF8=UTF8_code;
while (*p_UTF8)//统计UTF8数据中有多少个字符
{
p_UTF8++;
len_UTF8++;
}
p_UTF8=UTF8_code;
for (i=len_UTF8-1 ; i>=0; --i)
{
p_UTF8[i*2+1]=p_UTF8[i];
p_UTF8[i*2]='%';
}
// vm_gb2312_to_ucs2(url,500,"http://wap.baidu.com/tq?&ssid=0&from=0&area=");
p_UTF8=UTF8_code;
// sprintf(path, "%c://vre", driveName);//盘符名 driveName
sprintf(url,"%s","http://wap.baidu.com/tq?&ssid=0&from=0&area=");
for (i=0;i
sprintf(url, "%s%c%x", url,UTF8_code[i], UTF8_code[i + 1]);
i += 2;
}
vm_gb2312_to_ucs2(wurl, 500, url);
// strcat((VMCHAR*)url,(VMCHAR*)url2);
vm_open_wap_url(wurl);
// vm_gb2312_to_ucs2(url, 50, "http://www.baidu.com");
// vm_open_wap_url(url);
//vm_gb2312_to_ucs2(wurl,500*sizeof(VMWCHAR),url);
//strcat(url,"&word=");
//strcat(url,vm_ucs2_string("刘德华"));
// strcat(url,"&EQID=");
// vm_get_sys_property(VRE_SYS_EQUIPMENT_ID,vale,20);
// strcat(url,vale);
//
// strcat(url,"&USERID=");
// vm_get_sys_property(VRE_SYS_SUBSCRIBER_ID,vale,20);
// strcat(url,vale);
// strcat(url," //VRE版本号:");
// vm_get_sys_property(VRE_SYS_VERSION,vale,20);
// strcat(url,vale);
//
// strcat(url," //宿主平台版本号:");
// vm_get_sys_property(VRE_SYS_HOST_VERSION,vale,20);
// strcat(url,vale);
//
// strcat(url," //最大内存:");
// vm_get_sys_property(VRE_SYS_HOST_MAX_MEM,vale,20);
// strcat(url,vale);
//
// strcat(url," //home路径:");
// vm_get_sys_property(VRE_SYS_HOME_DIR,vale,20);
// strcat(url,vale);
//strcat(url," ");
//vm_log_error(url);
}
/************************************************************************/
/*
UCS2toUTF8Code 编码转换
*/
/************************************************************************/
static VMUINT8 UCS2toUTF8Code(VMUINT16 *ucs2_code, VMUINT8* utf8_code){
VMINT length = 0;
VMINT counter = 0;
VMINT step = 0;
VMINT end = wstrlen((VMWCHAR *)ucs2_code);
VMUINT16 tmp = *ucs2_code;
VMUINT8* out = utf8_code;
if(!utf8_code){
return length;
}
for(counter = 0; counter < end; counter++) {
if(0x0080 > *ucs2_code){
/* 1 byte UTF-8 Character.*/
*out = (VMUINT8)(tmp);
length++;
step = 1;
ucs2_code ++;
tmp = *(ucs2_code);
out += step;
}
else if(0x0800 > *ucs2_code){
if(*(out - 1) == 20) {
*out = (VMUINT8)(tmp);
length++;
step = 1;
ucs2_code ++;
tmp = *(ucs2_code);
out += step;
continue;
}
/*2 bytes UTF-8 Character.*/
*out = ((VMUINT8)(tmp >> 6)) | 0xc0;
*(out+1) = ((VMUINT8)(tmp & 0x003F)) | 0x80;
length += 2;
step = 2;
ucs2_code ++;
tmp = *(ucs2_code);
out += step;
}
else {
/* 3 bytes UTF-8 Character .*/
*out = ((VMUINT8)(tmp >> 12)) | 0xE0;
*(out+1) = ((VMUINT8)((tmp & 0x0FC0)>> 6)) | 0x80;
*(out+2) = ((VMUINT8)(tmp & 0x003F)) | 0x80;
length += 3;
step = 3;
ucs2_code ++;
tmp = *(ucs2_code);
out += step;
}
}
out++;
*out = 0;
return ++length;
}