VC\MFC当中CString、string、char、char*、char数组、int等类型之间的转换令人晕头转向,特地搜集多篇文章资料,利用代码实例等清晰的理清他们之间的关系和如何转换,其实非常简单。

1、CString std::string互转
std::string strstring = "std::string";
CString strCString = _T("CString");

//CString->std::string
strstring = CStringA(strCString);

//std::string->CString
strCString = strstring.c_str();

2、int和CString
//CString->int
CString strInt = _T("123");
int n = 0;
n = _ttoi(strInt);
n = 9;
//int->CString
strInt.Format(_T("%d"), n);

3、double和CString
CString strdouble = _T("123.00");
double dVal = 0;
//CString->double
dVal = _tstof(strdouble);
dVal = 999.0;
//double->CString
strdouble.Format(_T("%f"), dVal);

//4、char*和CString
char* chchar = "chahdaslhdf";
CString strCh = _T("char");
//char*->CString
strCh = chchar;
//或者
//预转换,得到所需空间的大小
int wcsLen = ::MultiByteToWideChar(CP_ACP, NULL, chchar, strlen(chchar), NULL, 0);
//分配空间要给'\0'留个空间,MultiByteToWideChar不会给'\0'空间
wchar_t* wszString = new wchar_t[wcsLen + 1];
//转换
::MultiByteToWideChar(CP_ACP, NULL, chchar, strlen(chchar), wszString, wcsLen);
//最后加上'\0'
wszString[wcsLen] = '\0';
//附加到CString对象上
CString content;
content.Append(wszString);

//CString->char*
int n = strCh.GetLength(); //按字符计算,strCh的长度

int len = WideCharToMultiByte(CP_ACP, 0, strCh, n, NULL, 0, NULL, NULL);//按Byte计算str长度
char *pChStr = new char[len+1];//按字节为单位
memset(pChStr,0,len+1);
WideCharToMultiByte(CP_ACP, 0, strCh, n, pChStr, len, NULL, NULL);//宽字节转换为多字节编码
pChStr[len] = '\0';
memcpy(chDis, pChStr, len + 1);
delete []pChStr;

CString转char*

char* ch = new char;
CString str1 = _T("12121");
strcpy(ch, CStringA(str1).GetString());

5、将wchar_t*转化为char*

char* UnicodeToAnsi(const wchar_t* szStr)
{
    int nLen = WideCharToMultiByte( CP_ACP, 0, szStr, -1, NULL, 0, NULL, NULL );
    if (nLen == 0)
    return NULL;

char* pResult = new char[nLen+1];
    WideCharToMultiByte( CP_ACP, 0, szStr, -1, pResult, nLen, NULL, NULL );
    return pResult;
}

6、将char*转化为wchar_t*

wchar_t* AnsiToUnicode(const char* szStr)
{
    int nLen = MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, szStr, -1, NULL, 0 );
    if (nLen == 0)
    return NULL;

wchar_t* pResult = new wchar_t[nLen+1];
    MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, szStr, -1, pResult, nLen );
    return pResult;
}

7、头文件#include <sstream>
//int->string
int n1 = 0;
std::stringstream ssCov;
std::string str;
ssCov<<n1;
ssCov>>str;

//string->int
std::string strNum = "123";
int n2 = atoi(strNum.c_str());

Unicode编码下字符串转换的更多相关文章

  1. 字符编码(续)---Unicode与ANSI字符串转换以及分辨字符编码形式

    Unicode与ANSI字符串转换 我们使用windows函数MultiByteToWideChar将多字节字符串转换为宽字符字符串,如下: int MultiByteToWideChar( UINT ...

  2. php unicode编码和字符串互转

    php字符串转Unicode编码, Unicode编码转php字符 百度了很多,都一样, 要么不对, 要不就是只是把字符串的汉字转Unicode 经过多次试验查找, 找到了如下方法, 注意:字符串编码 ...

  3. Unicode编码解码在线转换工具

    // Unicode编码解码在线转换工具 Unicode 是基于通用字符集(Universal Character Set)的标准来发展,并且同时也以书本的形式(The Unicode Standar ...

  4. Gson字符串编码,字符串转换成图片保存,二进制转换成图片保存

    import java.io.BufferedInputStream; import java.io.ByteArrayInputStream; import java.io.File; import ...

  5. C++下字符串转换

    引用自:http://blog.sina.com.cn/s/blog_a98e39a20101ari9.html 把最近用到的各种unicode下类型转换总结了一下,今后遇到其他的再补充: 1.str ...

  6. utf8、ansii、unicode编码之间的转换

    #include "stdafx.h"#include "windows.h"#include <iostream>#include <str ...

  7. .Net(c#)汉字和Unicode编码互相转换

    {"Tilte": "\u535a\u5ba2\u56ed", "Href": "http://www.cnblogs.com&q ...

  8. .Net(c#)汉字和Unicode编码互相转换实例

    {"name": "\u676d\u5dde", "href": "www.baidu.com"} 经常遇到这样内容的j ...

  9. Unicode编码解析

    概述 转载自博文浅谈Unicode编码 参考博文,结合自己实际情况进行部分内容添加修改 一 需求 java源码中尤其是数字部分用到了很多Unicode方面知识,如果不懂,看源码的时候会很懵逼 java ...

随机推荐

  1. Linux就该这么学--Shell脚本条件语句(一)

    1.条件测试语句能够让Shell脚本根据实际工作灵活调整工作内容,例如判断系统的状态后执行指定的工作,或创建指定数量的用户,批量修改用户密码,这些都可以让Shell脚本通过条件测试语句完成. if条件 ...

  2. smartforms 条码打印

    转自:http://blog.csdn.net/zhongguomao/article/details/6759642 SAP 条码打印有 新旧 两种技术方法:1.传统的(旧):将数据发送到打印机上, ...

  3. OSGI简介(转)

    原文地址 目前,业内关于OSGI技术的学习资源或者技术文档还是很少的.我在某宝网搜索了一下“OSGI”的书籍,结果倒是有,但是种类少的可怜,而且几乎没有人购买.因为工作的原因我需要学习OSGI,所以我 ...

  4. centos下更换默认的python版本

    1.在命令行输入python查看版本 2.到官网下载自己的python版本 wget https://www.python.org/ftp/python/3.5.1/Python-3.5.1.tgz ...

  5. 微信小程序开发:学习笔记[1]——Hello World

    微信小程序开发:学习笔记[1]——Hello World 快速开始 1.前往微信公众平台下载微信开发者工具. 地址:https://mp.weixin.qq.com/debug/wxadoc/dev/ ...

  6. Machine Learning No.5: Neural networks

    1. advantage: when number of features is too large, so previous algorithm is not a good way to learn ...

  7. 算法(Algorithms)第4版 练习 1.3.15

    Queue: package com.qiusongde; import java.util.Iterator; import java.util.NoSuchElementException; im ...

  8. 一篇文章教你如何用R进行数据挖掘

    一篇文章教你如何用R进行数据挖掘 引言 R是一种广泛用于数据分析和统计计算的强大语言,于上世纪90年代开始发展起来.得益于全世界众多 爱好者的无尽努力,大家继而开发出了一种基于R但优于R基本文本编辑器 ...

  9. stack_1.设计一个有getMin功能的栈

    思路 : 生成两个栈($stack ,$stack_min ),往$stack塞数据($value)的时候 ,比较一下$value和$stack_min最上面的元素的大小,如果$value小,则压入$ ...

  10. python快速上手教程

    python版本 python目前的版本分为2.7和3.5,两种版本的代码目前无法兼容,查看python版本号: python --version 基本数据类型 数字类型 整型和浮点型数据和其它编程语 ...