UTF8编码转换为汉字

mao_pu_hua 2010-08-02 02:39:31
%E4%B8%AD%E5%9B%BD%E9%93%B6%E8%A1%8C%E6%AD%A6%E6%B1%89%E5%B8%82%E9%87%91%E8%9E%8D%E6%B8%AF%E9%93%B6%E8%A1%8C
有这么一段数据,它是汉字“中国银行武汉市金融港银行”的UTF8编码,怎么将那串数据转换为这些汉字了。
我想用c语言实现,能够打印出那个银行名字出来。谢谢。

注意(上面的%号是不用的,可以去掉。谢谢!!!!)


顺便问一下,如果我知道某些汉字的unicoide 编码又怎么转换为汉字了。谢谢
...全文
1739 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
fordat 2012-03-01
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 hengyunabc 的回复:]
C/C++ code


#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <Windows.h>
using namespace std;
int main()
{
char s[] = "%E4%B8%AD%E5%9B%BD%E9%93%B6%E8%A1%8C%E6%AD%A6%E……
[/Quote]



纯学习:15楼的我编译了一下,说是变量 i 重复定义。疑问:好像标准c里面这样的定义局部变量没问题啊。怎么回事?
fordat 2012-03-01
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 mymtom 的回复:]
如需要阅读该回复,请登录或注册CSDN!
[/Quote]

纯学习:楼上的我编译了一下,说是变量 i 重复定义。疑问:好像标准c里面这样的定义局部变量没问题啊。怎么回事?
横云断岭 2010-08-02
  • 打赏
  • 举报
回复


#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <Windows.h>
using namespace std;
int main()
{
char s[] = "%E4%B8%AD%E5%9B%BD%E9%93%B6%E8%A1%8C%E6%AD%A6%E6%B1%89%E5%B8%82%E9%87%91%E8%9E%8D%E6%B8%AF%E9%93%B6%E8%A1%8C";

int len = strlen(s);
char* temps = new char[len+1];
temps[len] = '\0';

int temps_pos = 0;

//去掉%号
for (int i = 0;i<len;++i)
{
if (s[i] != '%')
{
temps[temps_pos] = s[i];
temps_pos++;
}
}
temps[temps_pos] = '\0';
cout<<temps<<endl;

int temps_len = strlen(temps);

int UTF8len = temps_len/2;
char* UTF8str = new char[UTF8len+1];

UTF8str[UTF8len] = '\0';
//把字符转换成数字,得到真实的UTF8串
for (int i = 0;i<UTF8len*2;)
{
char convert[3] = {0};
convert[0] = temps[i++];
convert[1] = temps[i++];
char *end;
int tempint = strtol(convert,&end,16);
UTF8str[i/2-1] = tempint;
}

//UTF8转换到UTF16
int wcslen = ::MultiByteToWideChar(CP_UTF8,NULL,UTF8str,UTF8len,NULL,0);

wchar_t* wszString = new wchar_t[wcslen + 1];

::MultiByteToWideChar(CP_UTF8,NULL,UTF8str ,UTF8len,wszString,wcslen);

wszString[wcslen] = L'\0';

//设置区域
std::wcout.imbue(std::locale("CHS"));
wcout<<wszString<<endl;

delete []temps;
delete []UTF8str;
delete []wszString;
}


mymtom 2010-08-02
  • 打赏
  • 举报
回复
cd = iconv_open("GB18030", "UTF-8");
这句需要用iconv -l查一下, 不同的系统中编码的名称可能不同
AIX: GB18030 : UTF-8
HP-UX: gb18300 : utf8 UTF8 UTF-8
mymtom 2010-08-02
  • 打赏
  • 举报
回复
用iconv

/**
* Copyright (C), 1988-2010
*
* $Id$
*
* $Log$
*/
#ifndef lint
static const char RCSID[] =
"$Id$";
static const char RELID[] =
"$" "Date: "__FILE__" "__DATE__" "__TIME__" $";
#endif /* not lint */

/**
* @file k_iconv.c
* @brief
*/

#include <stdio.h>
#include <string.h>
#include <iconv.h>

char *k_iconv_utf8_to_gb18030(const char *str)
{
iconv_t cd;
char *inbuf;
char *outbuf;
char dstbuf[1024];
size_t dstlen;
size_t size;
size_t inleft;
size_t outleft;

dstlen = sizeof(dstbuf);

cd = iconv_open("GB18030", "UTF-8");
if ((iconv_t)-1 == cd) {
return strdup((char *)str);
}


inbuf = (char *)str;
outbuf = dstbuf;
inleft = strlen(str);
outleft = dstlen;

size = iconv(cd, &inbuf, &inleft, &outbuf, &outleft);
iconv_close(cd);

if ((size_t)-1 == size) {
return strdup((char *)str);
}

if (inleft > 0) {
return strdup((char *)str);
}

dstbuf[dstlen - outleft] = 0;

return strdup((char *)dstbuf);
}

int
main(int argc, char *argv[])
{

char buf_utf8[] = "\xE4\xB8\xAD\xE5\x9B\xBD\xE9\x93\xB6\xE8\xA1\x8C\xE6\xAD\xA6\xE6\xB1\x89\xE5\xB8\x82\xE9\x87\x91\xE8\x9E\x8D\xE6\xB8\xAF\xE9\x93\xB6\xE8\xA1\x8C\x00";

char *buf_gb18030;
int i;

buf_gb18030 = k_iconv_utf8_to_gb18030(buf_utf8);
printf("%s\n", buf_gb18030);

for (i = 0; i < strlen(buf_utf8); i++)
printf("%%%X", (unsigned char)(unsigned char)(buf_utf8[i]));
printf("\n");

for (i = 0; i < strlen(buf_gb18030); i++)
printf("%%%X", (unsigned char)(unsigned char)(buf_gb18030[i]));
printf("\n");

return 0;
}
/* 输出:
中国银行武汉市金融港银行
%E4%B8%AD%E5%9B%BD%E9%93%B6%E8%A1%8C%E6%AD%A6%E6%B1%89%E5%B8%82%E9%87%91%E8%9E%8D%E6%B8%AF%E9%93%B6%E8%A1%8C
%D6%D0%B9%FA%D2%F8%D0%D0%CE%E4%BA%BA%CA%D0%BD%F0%C8%DA%B8%DB%D2%F8%D0%D0
*/
mao_pu_hua 2010-08-02
  • 打赏
  • 举报
回复
我是在linux环境下的。我的环境是utf8格式的。如果是在windows下面好像是unicode的吧。显示不了utf8吧
bobo364 2010-08-02
  • 打赏
  • 举报
回复
10楼,我这里试下来是乱码啊?中文还是用微软的api,不然就是不太正确。


#include<stdio.h>

int main()
{
unsigned char p[20] = {0xe4,0xb8,0xad,0xe5,0x9b,0xbd};
printf( "%s\n", p );
system("pause");
return 0;
}
mao_pu_hua 2010-08-02
  • 打赏
  • 举报
回复
上面贴出来的是utf8编码,而且我测试过
int main( void )
{
unsigned char p[20] = { 0xe4,0xb8,0xad,0xe5,0x9b,0xbd };
printf( "%s\n", p );
return 0;
}

结果是中国。你额可以测试一下
mao_pu_hua 2010-08-02
  • 打赏
  • 举报
回复
再linux机器上面utf8的确是可以直接打印出来。但是unicode是打印不出来的
mao_pu_hua 2010-08-02
  • 打赏
  • 举报
回复
我用的是unicode编码,直接打印的话是utf8肯定是打印不出来的
%D6%D0%B9%FA%D2%F8%D0%D0%CE%E4%BA%BA%CA%D0%CD%F8%D6%B7 它是unicode编码
它的中文是"中国银行武汉市网址",也就是我要把unicode转换为utf8编码,再打印出来
feifanup 2010-08-02
  • 打赏
  • 举报
回复
如果是utf8,而已用winapi转成unicode. 在支持中文的机器上可以看到汉字
横云断岭 2010-08-02
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 hengyunabc 的回复:]

晕,根本不是UTF8编码。。
[/Quote]
是UTF8。。。
zhengjiankang 2010-08-02
  • 打赏
  • 举报
回复
utf-8格式的你转换成unicode格式就可以了。
unicode格式的本来就已经是汉字了,直接打印出来就是汉字了。
不知道你想转换成什么,莫非在内存里面就转换成汉字?
如果想那样的话那么对不起了,计算机只认识0和1
zhengjiankang 2010-08-02
  • 打赏
  • 举报
回复
直接打印出来就是汉字了~不知道你要转化什么~
横云断岭 2010-08-02
  • 打赏
  • 举报
回复
晕,根本不是UTF8编码。。
mao_pu_hua 2010-08-02
  • 打赏
  • 举报
回复
在线等待谢谢
ForestDB 2010-08-02
  • 打赏
  • 举报
回复
友情顶。

69,381

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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