设为首页 加入收藏

TOP

c/c++ 中文字符串转Unicode和UTF8
2015-11-21 01:01:33 来源: 作者: 【 】 浏览:2
Tags:c/c 中文 字符串 Unicode UTF8

1. 描述

  在windows上做系统编程,少不了会遇到处理中文字符串的问题。而大多时候中文汉字都是以多字节编码的方式展现的。为了实现更好的兼容性或一些特殊的需求,(比如在网页上显示。)常需要将其转换成unicode或者utf8的格式。


2. 代码示例

2.1 中文字符串转Unicode

/************************************************************************
*int CN2Unicode(char *input,wchar_t *output)
*功能:中文字符转换为unicode字符
*参数:input,包含中文的字符串,output,Unicode字符串
*
*************************************************************************/
int CN2Unicode(char *input,wchar_t *output)
{
    int len = strlen(input);

    //wchar_t *out = (wchar_t *) malloc(len*sizeof(wchar_t));

    len=MultiByteToWideChar(CP_ACP,0,input,-1,output,MAX_PATH);

    return 1;
}

2.2 中文字符串转utf8

/************************************************************************
*int CN2Utf8(char *input,char *output)
*功能:中文字符串转换为utf8字符串
*参数:input,包含中文的字符串,output,utf8字符串
*
************************************************************************/
int CN2Utf8(char *input,char *output)
{
    int len ;
    wchar_t *out = (wchar_t *) malloc(len*sizeof(wchar_t));

    len = MultiByteToWideChar(CP_ACP,0,input,-1,out,strlen(input)+1);
    WideCharToMultiByte(CP_UTF8,0,out,wcslen(out),output,len,NULL,NULL);

    return 1;
}
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇nyoj114 某种序列(大数) 下一篇C++刷题――1924: 计算两点间的距..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: