linux 中文转unicode,Linux下汉字编码的转换

因为项目的需要linux下将GBK编码转换为utf8编码,google一下,网上的相关资源比较少,下面的操作经过本人的反复试验。本例子同样适用于其他的编码转换。

有gbk到utf8的转换过程,需要经过unicode作为中间编码。因为Windows的转换相对简单,先讲一下windows下的转换过程,linux下的过程基本相同,函数使用上有差别。

Windows下:

1、在windows下可以使用函数MultiByteToWideChar先将多字节字符,转换为unicode。

2、使用函数WideCharToMultiByte,将unicode再转换为utf8编码。

google一下,网上例子很多。在这里贴了一个简单的源码,实现ansi到utf8编码的转换

view plain

char *multichar_2_utf8(const char *m_string)

{

int len=0;

wchar_t *w_string;

char *utf8_string;

//计算由ansi转换为unicode后,unicode编码的长度

len=MultiByteToWideChar(CP_ACP,0,(LPCTSTR)m_string, -1, NULL,0);//CP_ACP指示了转换为unicode编码的编码类型

w_string=(wchar_t *)malloc(2*len+2);

memset(w_string,0,2*len+2);

//ansi到unicode转换

MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)m_string,-1,w_string, len);//CP_ACP指示了转换为unicode编码的编码类型

//计算unicode转换为utf8后,utf8编码的长度

len = WideCharToMultiByte(CP_UTF8, 0, w_string, -1, NULL, 0, NULL, NULL);//CP_UTF8指示了unicode转换为的类型

utf8_string=(char *)malloc(len+1);

memset(utf8_string, 0, len + 1);

//unicode到utf8转换

WideCharToMultiByte (CP_UTF8, 0, w_string, -1, utf8_string, len, NULL,NULL);//CP_UTF8指示了unicode转换为的类型

free(w_string);

return utf8_string;

}

Linux下:

linux下没有上面的两个函数,需要使用函数 mbstowcs和wcstombs

mbstowcs将多字节编码转换为宽字节编码

wcstombs将宽字节编码转换为多字节编码

这两个函数,转换过程中受到系统编码类型的影响,需要通过设置来设定转换前和转换后的编码类型。通过函数setlocale进行系统编码的设置。

linux下输入命名

locale -a查看系统支持的编码类型。

view plain

andy@andy-linux:~$ locale -a

C

en_AG

en_AU.utf8

en_BW.utf8

en_CA.utf8

en_DK.utf8

en_GB.utf8

en_HK.utf8

en_IE.utf8

en_IN

en_NG

en_NZ.utf8

en_PH.utf8

en_SG.utf8

en_US.utf8

en_ZA.utf8

en_ZW.utf8

POSIX

zh_CN.gb18030

zh_CN.gbk

zh_CN.utf8

zh_HK.utf8

zh_SG.utf8

zh_TW.utf8

本例子中实现的是由zh_CN.gbk到zh_CN.utf8的转换

流程:

1、调用函数setlocale(LC_ALL,"zh_CN.gbk"),设置待转码的字符串类型为gbk类型。

2、调用函数mbstowcs,实现 1 设置的编码到unicode编码的转换。

3、调用函数setlocale(LC_ALL,"zh_CN.utf8"),设置转换后编码类型为utf8类型。

4、调用函数wcstombs,实现unicode到 3 设置的编码类型的转换。

下面是我写的源码

view plain

#include

#include

/******************************************************************************

* FUNCTION: gbk2utf8

* DESCRIPTION: 实现由gbk编码到utf8编码的转换

*

* Input: utfStr,转换后的字符串; srcStr,待转换的字符串; maxUtfStrlen, utfStr的最

大长度

* Output: utfStr

* Returns: -1,fail;>0,success

*

* modification history

* --------------------

* 2011-nov-25, lvhongya written

* --------------------

******************************************************************************/

int gbk2utf8(char *utfStr,const char *srcStr,int maxUtfStrlen)

{

if(NULL==srcStr)

{

printf("Bad Parameter\n");

return -1;

}

//首先先将gbk编码转换为unicode编码

if(NULL==setlocale(LC_ALL,"zh_CN.gbk"))//设置转换为unicode前的码,当前为gbk编码

{

printf("Bad Parameter\n");

return -1;

}

int unicodeLen=mbstowcs(NULL,srcStr,0);//计算转换后的长度

if(unicodeLen<=0)

{

printf("Can not Transfer!!!\n");

return -1;

}

wchar_t *unicodeStr=(wchar_t *)calloc(sizeof(wchar_t),unicodeLen+1);

mbstowcs(unicodeStr,srcStr,strlen(srcStr));//将gbk转换为unicode

//将unicode编码转换为utf8编码

if(NULL==setlocale(LC_ALL,"zh_CN.utf8"))//设置unicode转换后的码,当前为utf8

{

printf("Bad Parameter\n");

return -1;

}

int utfLen=wcstombs(NULL,unicodeStr,0);//计算转换后的长度

if(utfLen<=0)

{

printf("Can not Transfer!!!\n");

return -1;

}

else if(utfLen>=maxUtfStrlen)//判断空间是否足够

{

printf("Dst Str memory not enough\n");

return -1;

}

wcstombs(utfStr,unicodeStr,utfLen);

utfStr[utfLen]=0;//添加结束符

free(unicodeStr);

return utfLen;

}

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用Python内置的decode()函数将Unicode编码转换汉字。例如,如果要将Unicode编码为"\u4e2d\u6587"转换汉字,可以使用以下代码: unicode_str = "\u4e2d\u6587" chinese_str = unicode_str.encode('utf-8').decode('unicode_escape') print(chinese_str) 输出结果为:"中文"。 ### 回答2: 在Python中,我们可以使用`chr()`函数将Unicode编码转换汉字Unicode是一种标准化的字符集,它为世界上几乎所有的字符定义了独一无二的数值编码。而汉字Unicode字符集中的一部分。 要将Unicode编码转换汉字,可以使用以下代码: ```python unicode_code = 27721 # 要转换Unicode编码 hanzi = chr(unicode_code) # 使用chr()函数将Unicode编码转换汉字 print(hanzi) # 输出转换后的汉字 ``` 在上面的代码中,`unicode_code`是要转换Unicode编码,可以根据需要更改此值。`chr()`函数用于将Unicode编码转换为对应的字符。 当我们运行上述代码时,将输出转换后的汉字。 需要注意的是,转换的前提是指定的Unicode编码对应于汉字字符。在Unicode中,汉字字符的编码范围是0x4E00到0x9FFF,您可以根据需要更改`unicode_code`变量的值来指定不同的汉字编码。 另外,还可以使用`ord()`函数将汉字转换Unicode编码。使用方法与`chr()`函数相反。具体代码如下: ```python hanzi = '你' # 要转换汉字 unicode_code = ord(hanzi) # 使用ord()函数将汉字转换Unicode编码 print(unicode_code) # 输出转换后的Unicode编码 ``` 以上就是使用Python将Unicode编码转换汉字的方法。 ### 回答3: 在Python中将Unicode编码转换汉字可以使用`chr()`函数。`chr()`函数将Unicode编码作为参数,返回对应的字符。使用以下步骤将Unicode编码转换汉字。 首先,确定Unicode编码的值。例如,要将Unicode编码`U+6C49`转换汉字。 ```python unicode_code = 0x6C49 ``` 然后,使用`chr()`函数将Unicode编码转换为对应的字符。 ```python chinese_character = chr(unicode_code) ``` 最后,打印输出转换后的汉字。 ```python print(chinese_character) ``` 运行代码后,将输出转换后的汉字。 完整的代码示例: ```python unicode_code = 0x6C49 chinese_character = chr(unicode_code) print(chinese_character) ``` 以上代码将输出`汉`,即Unicode编码`U+6C49`对应的汉字。 需要注意的是,Python默认使用的是UTF-8编码,因此在处理Unicode编码时,确保当前环境的编码设置正确。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值