wchar_t char string wstring 之间的转换
wchar_t char string wstring 之间的转换
转:http://blog.csdn.net/lbd2008/article/details/8333583
在处理中文时有时需要进行wchar_t,char,string,wstring之间的转换。
其中char和string之间、wchar_t和wstring之间的转换较为简单,代码在vs2010下测试通过。
#include <iostream>
#include <string>
#include <tchar.h>
#include <Windows.h> using namespace std; //Converting a WChar string to a Ansi string
char *w2c(char *pcstr,const wchar_t *pwstr, size_t len)
{
int nlength=wcslen(pwstr);
//获取转换后的长度
int nbytes = WideCharToMultiByte( , , pwstr, nlength, NULL,,NULL, NULL );
if(nbytes>len) nbytes=len;
// 通过以上得到的结果,转换unicode 字符为ascii 字符
WideCharToMultiByte( ,, pwstr, nlength, pcstr, nbytes, NULL, NULL );
return pcstr ;
} int main(){ setlocale(LC_ALL,"chs");
char* cc = "this is a char 测试";
wchar_t* wcc = L"this is a wchar 测试";
string str("this is a string 测试 ");
wstring wstr = L"this is a wstring 测试"; //string to char
const char* char_test = str.c_str();
//cout<<"char_test:"<<char_test<<endl; //char to string
string ss = cc;
//cout<<"ss is :"<<ss<<endl; //wstring to wchar
const wchar_t* wchar_test = wstr.c_str();
//wcout<<wchar_test<<endl; //wchar to wstring
wstring wss = wcc;
wcout<<wcc<<endl; //char to wchar_t
wchar_t *wc = new wchar_t[str.size()+];
//swprintf(wc,L"%S",cc);
//wcout<<cc<<endl;
delete []wc; // wchar_t to char
char *pcstr = (char *)malloc(sizeof(char)*( * wcslen(wcc)+));
memset(pcstr , , * wcslen(wcc)+ );
w2c(pcstr,wcc, * wcslen(wcc)+) ;
free(pcstr); system("pause");
return ;
}
wchar_t char string wstring 之间的转换的更多相关文章
- wchar_t,char,string,wstring等的总结
一.LPSTR LPCSTR LPTSTR LPCTSTR等 确定的类型: LPSTR = CHAR * = char * LPCSTR = const CHAR * = char * //c意为co ...
- Char* ,CString ,WCHAR*之间的转换
关于Char* ,CString ,WCHAR*之间的转换问题 GDI+所有类的接口函数如果要传递字符串作为参数的话,似乎都用UNICODE串,即WCHAR*.我开始也被整得晕头转向,因为窗口编程所用 ...
- QString和char字符串数组之间的转换 (转)
做串口通信时,碰到这样的问题,在Qt的界面中,我用QLineEdit对象负责显示发送和接收的文本,对其中的数据进行读取和显示使用的数据类型都是QString:但是,在对串口设备进行读写时,数据使用的形 ...
- Java基本数据类型、包装类与String类之间的转换
一.基本数据类型与包装类之间的转换: import org.junit.Test; public class MainTest { /** * 基本数据类型与包装类之间的转换 */ @Test pub ...
- 关于Char* ,CString ,WCHAR*之间的转换问题
GDI+所有类的接口函数如果要传递字符串作为参数的话,似乎都用UNICODE串,即WCHAR*.我开始也被整得晕头转向,因为窗口编程所用往往是CString,用IO流读文件数据又得到char *.得益 ...
- c++中char*\wchar_t*\string\wstring之间的相互转换
string U2A(const wstring& str)//Unicode字符转Ascii字符 { string strDes; if ( str.empty() ) goto __end ...
- string与wstring之间的转换
#include <string>std::string ws2s(const std::wstring& ws){ std::string curLocale = setl ...
- char*,wchar_t*,CString和BSTR之间的转换
前言 本文并不尝试列举出所有的转换方法,只列举作者认为方便易用的方法. 1.char*和wchar_t*的相互转换 可以利用中间类_bstr_t(头文件comdef.h)方便的进行相互转换 const ...
- QString和char字符数组之间的转换(QTextCodec.toUnicode方法,特别注意\0的问题)
How can I convert a QString to char* and vice versa ?(trolltech) Answer:In order to convert a QStrin ...
随机推荐
- MVC中定时发布二维码邮件
发布邮件 查看第一个方法就可以了,第二个跟这个无关 using System; using System.Collections.Generic; using System.Linq; using S ...
- Spring思维导图,让spring不再难懂(一)
写在前面 很多人在微信公众号中给我留言说想看spring的思维导图,正好也打算写.与其他框架相比,spring项目拥有更多的模块,我们常用的ioc,mvc,aop等,这些是spring的主要板块.一篇 ...
- 利用Html.css OPPO手机导航菜单的制作解析
<body> <div id="top" class="auto"> <div class="nav"> ...
- jar插件应用
Gson(解析json) 作用:在servlet层中解析json 1:导入jar包 gson-2.2.4.jar 例如:Gson gson = new Gson(); ...
- tenserflow models包的安装 123
1.下载 models包 https://github.com/tensorflow/models 2.将models包拷贝到本机Python包的安装地址即可,本机Python包的安装地址的查看方式可 ...
- Leetcode 16
//一次AC 有点爽的class Solution { public: int threeSumClosest(vector<int>& nums, int target) { ; ...
- 改变Vim保存文件路径
1. vim 有个cd命令.用来更改当前文件夹.:cd sth进入sth文件夹.这样新文件保存之后就在当前文件夹.不过如果你打开一个已经保存的文件后然后更改当前文件夹是不会改变保存路径的.你必须为:w ...
- SGU 140. Integer Sequences 线性同余,数论 难度:2
140. Integer Sequences time limit per test: 0.25 sec. memory limit per test: 4096 KB A sequence A is ...
- 2019年微信小程序1月TOP100榜单
- HTML5和XHTML的区别
既然被问到了HTML5和XHTML的区别,那我就在这里给大家分享一些我个人的理解,同时我也觉得从他们的来源上讲,他们藏着一个有趣的故事. 首先认识三个组织,IETF (Internet Enginee ...