vc++字符转换

测试环境: vs2008

开发语言:C++

#include <iostream>
#include <windows.h>
#include
<string>

// wchar_t to string
void
Wchar_tToString(std::string& szDst, wchar_t *wchar)
{
wchar_t *
wText = wchar;
DWORD dwNum =
WideCharToMultiByte(CP_OEMCP,NULL,wText,-1,NULL,0,NULL,FALSE);//
WideCharToMultiByte的运用
char *psText; //
psText为char*的临时数组,作为赋值给std::string的中间变量
psText = new
char[dwNum];
WideCharToMultiByte
(CP_OEMCP,NULL,wText,-1,psText,dwNum,NULL,FALSE);//
WideCharToMultiByte的再次运用
szDst = psText;// std::string赋值
delete
[]psText;// psText的清除
}

// string to
wstring
void StringToWstring(std::wstring& szDst,
std::string str)
{
std::string temp = str;
int
len=MultiByteToWideChar(CP_ACP, 0, (LPCSTR)temp.c_str(), -1,
NULL,0); 
wchar_t * wszUtf8 = new
wchar_t[len+1]; 
memset(wszUtf8, 0, len * 2 +
2); 
MultiByteToWideChar(CP_ACP, 0, (LPCSTR)temp.c_str(), -1,
(LPWSTR)wszUtf8, len);
szDst = wszUtf8;
std::wstring r =
wszUtf8;
delete[] wszUtf8;
}

int main(int argc, char
*argv)
{

// wchar_t  to string
std::string
szDest;
wchar_t wText[20] =
{L"宽字符转换字符串实例!OK!"};
Wchar_tToString(szDest,
wText);
std::cout<<"szDest:
"<<szDest<<std::endl;

/**
*wchar_t
定义的变量为什么不能输出呢?
*宽字符类型要本地化,否则输不出想要结果。
*本地化有三条语句可以使用,任取其一。最后一句是全局函数,前两个是wcout的一个成员函数的两种表达方式。
*/
//std::wcout.imbue(std::locale("chs"));
//std::wcout.imbue(std::locale(""));
setlocale(LC_ALL,"Chinese-simplified");

// string to wstring
std::string szSrc =
"字符串转换宽字符实例!OK!";
std::wstring wszDest;
StringToWstring(wszDest,
szSrc);
std::wcout << "wszDest: " << wszDest <<
std::endl;

/**
    string to wchar_t
 */

string str= "字符串转换宽字符实例!OK!";
std::wstring widstr =
std::wstring(str.begin(), str.end());
const wchar_t *pwidstr =
widstr.c_str();

// 此方法简单,但是 delete wc;时会出异常
wchar_t   *
  wc   =   new  
wchar_t[szSrc.size()]; 
swprintf(wc,L"%S
",szSrc.c_str());//注意大写S。。 
std::wcout <<"szSrc: " << wc
<<std::endl;
//delete wc;

system("PAUSE");
}

时间: 2024-08-01 19:34:10

vc++字符转换的相关文章

vc字符串转换处理:(绝对精华,收集所有的例子)

vc字符串转换处理:(绝对精华,收集所有的例子) 1.头文件中要定义宏; #define   UNICODE         #define   _UNICODE     //////////////////// char   buf[128];    memset(buf,0,128);     strcpy(buf,"你好");     WCHAR   pCName[128];     memset(pCName,0,128);     MultiByteToWideChar(CP

通过编写串口助手工具学习MFC过程&mdash;&mdash;(三)Unicode字符集的宽字符和多字节字符转换

通过编写串口助手工具学习MFC过程 因为以前也做过几次MFC的编程,每次都是项目完成时,MFC基本操作清楚了,但是过好长时间不再接触MFC的项目,再次做MFC的项目时,又要从头开始熟悉.这次通过做一个串口助手再次熟悉一下MFC,并做了一下记录,以便方便以后查阅.做的过程中多是遇到问题直接百度和谷歌搜索来的,所以很多都是不求甚解,知其然不知其所以然.另外做此工具只是为了熟悉了解,许多功能还没有完善!(开发工具VS2008) (三)Unicode字符集的宽字符和多字节字符转换 在上一节<(二)通过&qu

字符编码、字符存储、字符转换及工程中字符的使用

字符编码.字符存储.字符转换及工程中字符的使用 版本控制 版本 时间(北京时间) 作者 备注 V1.0 2016-05-13 施小丰 创建本文.第七章工程总结尚未完成 一.          前言 1.        目的 本文主要用于整理字符相关知识,包括字符编码.字符存储.行业标准.文件读写.工程注意事项等涉及字符相关的内容, 从而在实际工程中更好地设计和使用字符.更快地解决字符问题. 2.        适用范围 本文标题是"Windows C++字符编码.存储.转换大全", 但

java GBK字符转换成为UTF-8编码字符

import java.util.HashMap; import java.util.Map; /** * 创建日期: 2014-04-18 10:36:25 * 作者: 黄飞 * mail:[email protected] [email protected] * ©版权归作者所有 * */ public class ConverFromGBKToUTF8 { public static void main(String[] args) { try { ConverFromGBKToUTF8

*数组-01. 字符转换

1 /* 2 * Main.c 3 * E1-数组-01. 字符转换 4 * Created on: 2014年8月20日 5 * Author: Boomkeeper 6 ******部分通过********** 7 */ 8 9 #include <stdio.h> 10 11 int main(void) { 12 13 char str[80] = { -1 }; 14 15 gets(str); 16 17 int i; 18 for (i = 0; i < 80; i++)

发送邮件字符转换

//將html轉為text public string HtmlToText(string strContent) { strContent = strContent.Replace("&amp", "&"); strContent = strContent.Replace("''", "'"); strContent = strContent.Replace("&lt", &quo

IO-04字节-字符转换流

掌握OutputStreamWriter和InputStreamReader的作用. 在整个IO包中,实际上就是分为字节流和字符流,但除了这两个流之外,还存在一组字节-字符流转换类. OutputStreamWriter:是Writer的子类,将输出的字符流编程字节流,既:将一个字符流的输出对象变成字节流输出对象. InputStreamReader:是Reader的子类,将输入的字节流变成字符流.既:将一个字节流的输入对象变成字符流的输入对象. 字符流转换成字节流的代码: package li

HDOJ/HDU 2140 Michael Scofield&#39;s letter(字符转换~)

Problem Description I believe many people are the fans of prison break. How clever Michael is!! In order that the message won't be found by FBI easily, he usually send code letters to Sara by a paper crane. Hence, the paper crane is Michael in the he

1.字符转换

1.将单字节Char转化为双字节的wchar_t的转换函数 wchar_t* c2w(const char *str){     int length = strlen(str)+1;     wchar_t *t = (wchar_t*)malloc(sizeof(wchar_t)*length);     memset(t,0,length*sizeof(wchar_t));     MultiByteToWideChar(CP_ACP,0,str,strlen(str),t,length)