目录

第1章说明    1

1.1 代码    1

1.2 使用    4

第1章说明

VC++中宽窄字符串的相互转换比较麻烦,借助std::string能大大减少代码量。

1.1 代码

函数声明如下:

std::string stringA2W(const char* pA,int nA,UINT uCodePage = CP_ACP);

std::string stringW2A(const wchar_t*pW,int nW,UINT uCodePage = CP_ACP);

std::string stringA2W(const std::string&sA,UINT uCodePage = CP_ACP);

std::string stringW2A(const std::string&sW,UINT uCodePage = CP_ACP);

函数实现如下:

/***************************************************************\

窄字符串 ==> 宽字符串(UTF16LE)

pA [in] 窄字符串首地址

nA [in] 窄字符串长度。小于零就使用 strlen 计算长度。

uCodePage [in] 窄字符串的代码页

如:CP_ACP 表示系统默认;936 表示 GBK……

返回:

宽字符串 sW

宽字符串的字符数 nChar = sW.length() / sizeof(wchar_t)

宽字符串的字节数 nByte = (nChar + 1) * sizeof(wchar_t) - 1

字节数多加了 sizeof(wchar_t) - 1 = 1 个 \0,是为了下面的用法

const wchar_t* pW = (const wchar_t*)sW.c_str();

\***************************************************************/

std::string stringA2W(const char*pA,int nA,UINT uCodePage)

{

std::string sW;

if(pA)

{

if(nA < 0)

{

nA = strlen(pA);

}

if(nA > 0)

{

int nW = MultiByteToWideChar(uCodePage,0,pA,nA,NULL,0);

if(nW > 0)

{

int nByte = (nW + 1) * sizeof(wchar_t);

wchar_t*pW = (wchar_t*)malloc(nByte);

if(pW)

{

MultiByteToWideChar(uCodePage,0,pA,nA,pW,nW);

pW[nW] = L'\0';

sW.assign((const char*)pW,nByte - 1);

free(pW);

}

}

}

}

if(sW.empty())

{

sW = std::string(sizeof(wchar_t) - 1,'\0');

}

return sW;

}

/***************************************************************\

窄字符串 ==> 宽字符串(UTF16LE)

sA [in] 窄字符串

uCodePage [in] 窄字符串的代码页

如:CP_ACP 表示系统默认;936 表示 GBK……

返回:宽字符串

\***************************************************************/

std::string stringA2W(const std::string&sA,UINT uCodePage)

{

return stringA2W(sA.c_str(),sA.length(),uCodePage);

}

/***************************************************************\

宽字符串(UTF16LE) ==> 窄字符串

pW [in] 宽字符串首地址

nW [in] 宽字符串字符数。小于零就使用 wcslen 计算长度。

uCodePage [in] 窄字符串的代码页

如:CP_ACP 表示系统默认;936 表示 GBK……

返回:窄字符串

\***************************************************************/

std::string stringW2A(const wchar_t*pW,int nW,UINT uCodePage)

{

std::string sA;

if(pW)

{

if(nW < 0)

{

nW = wcslen(pW);

}

if(nW > 0)

{

int nA = WideCharToMultiByte(uCodePage,0,pW,nW

,NULL,NULL,NULL,NULL);

if(nA > 0)

{

char*pA = (char*)malloc(nA);

if(pA)

{

WideCharToMultiByte(uCodePage,0,pW,nW

,pA,nA,NULL,NULL);

sA.assign(pA,nA);

free(pA);

}

}

}

}

return sA;

}

/***************************************************************\

宽字符串(UTF16LE) ==> 窄字符串

sW [in] 宽字符串,编码为 UTF16LE

uCodePage [in] 窄字符串的代码页

如:CP_ACP 表示系统默认;936 表示 GBK……

返回:窄字符串

\***************************************************************/

std::string stringW2A(const std::string&sW,UINT uCodePage)

{

return stringW2A((const wchar_t*)sW.c_str()

,sW.length() / sizeof(wchar_t),uCodePage);

}

1.2 使用

有了上述四个函数,字符串的编码转换用一、两行代码即可实现。

如:将GBK字符串"测试"转换为宽字符串

std::string sW = stringA2W("测试");

//简体中文 Windows 下

std::string sW = stringA2W("测试",936);

//安装有代码页936的Windows

如:将GBK字符串"测试"转换为UTF-8编码

std::string sUTF8 = stringW2A(stringA2W("测试",936),CP_UTF8);

如:将GBK字符串"测试"转换为Big5编码

std::string sBig5 = stringW2A(stringA2W("测试",936),950);

VC++ 中使用 std::string 转换字符串编码的更多相关文章

  1. 基于std::string的字符串处理

    转自:http://zxdflyer.blog.163.com/blog/static/25664262201322510217495/ C++标准模板库std使用广泛.该库中处理字符串的对象为std ...

  2. C# 中怎么将string转换成int型

    int intA = 0;1.intA =int.Parse(str);2.int.TryParse(str, out intA);3.intA = Convert.ToInt32(str);以上都可 ...

  3. VC++中 wstring和string的互相转换实现

    在VC++开发中,经常会用到string和wstring,这就需要二者之间的转换,项目中封装了wstring和string相互转换的2个函数,实现如下: //将wstring转换成string std ...

  4. Python3中转换字符串编码

    在使用subprocess调用Windows命令时,遇到了字符串不显示中文的问题,源码如下:#-*-coding:utf-8-*-__author__ = '$USER' #-*-coding:utf ...

  5. 在Linux下使用iconv转换字符串编码

    在Linux下写C程序,尤其是网络通信程序时经常遇到编码转换的问题,这里要用到iconv函数库. iconv函数库有以下三个函数 123456 #include <iconv.h>icon ...

  6. C++ std::string 在一个字符串前插入一个字符串几种方式

    目录 1.直接使用字符串相加 2.使用insert函数 比较:通过Quick C++ Benchmarks 可得到结果 1.直接使用字符串相加 std::string a = "hello& ...

  7. java获取字符串编码和转换字符串编码

    public class EncodingUtil { // 这里可以提供更多地编码格式,另外由于部分编码格式是一致的所以会返回 第一个匹配的编码格式 GBK 和 GB2312 public stat ...

  8. (二)javascript中int和string转换

    在javascript里怎么样才能把int型转换成string型 (1)var x=100 a = x.toString() (2)var x=100; a = x +""; // ...

  9. 在java中,将String类型字符串s赋值为null后,将字符串与其他字符串拼接后得到结果出现了null字符串与其他字符连接的样式

    String s = null; s  += "hello"; System.out.println(s); 结果为:nullhello 原因: 先应用String.valueOf ...

随机推荐

  1. Codeforces Round #366 (Div. 2) B

    Description Peter Parker wants to play a game with Dr. Octopus. The game is about cycles. Cycle is a ...

  2. FTP协议标准命令

    FTP:文件传输协议(File Transfer Protocol) 文件传输协议(FTP)使得主机间可以共享文件.FTP 使用 TCP 生成一个虚拟连接用于控制信息,然后再生成一个单独的 TCP 连 ...

  3. 2016年10月24日 星期一 --出埃及记 Exodus 19:8

    2016年10月24日 星期一 --出埃及记 Exodus 19:8 The people all responded together, "We will do everything th ...

  4. php 修改、增加xml结点属性的实现代码

    php修改xml结点属性,增加xml结点属性的代码,有需要的朋友可以参考下 php 修改 增加xml结点属性的代码,供大家学习参考.php修改xml结点属性,增加xml结点属性的代码,有需要的朋友,参 ...

  5. java.lang.NoClassDefFoundError: org/w3c/dom/ElementTraversal

    今天用maven编写Selenium测试程序时,调用 HtmlUnitDriver driver = new HtmlUnitDriver(true); 反法时报错如下: java.lang.NoCl ...

  6. 关于mysql varchar 类型的最大长度限制

    Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. This ...

  7. windows8运行php Composer出现SSL报错的问题

    这是没有安装CA证书导致的!!! CA证书下载地址:http://curl.haxx.se/docs/caextract.html 然后修改php.ini文件 openssl.cafile= D:/w ...

  8. CentOS 6.5中linux grub修复

    在使用Linux的过程中,难免会出现开机提示grub >而无法启动,可能是系统中/boot/grub文件丢失等原因造成的,当出现此问题的时候只要系统分区没有格式化一般是可以修复的,下面就以虚拟 ...

  9. 从exchange2010上面删除特定主题或特定时间的邮件

    昨天在上班的公交上接到同事电话,说他的的部门老大发错了一封邮件到另外一个同事邮箱了,问我能不 能去那个同事的邮箱里面删除,我一想,之前在网上看到过资料,到了公司趁那个误接收邮件的同事还没有来,在服务器 ...

  10. CodeForces 451C Predict Outcome of the Game

    Predict Outcome of the Game Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d &a ...