如何:在各种字符串类型之间进行转换
[MSDN:http://msdn.microsoft.com/zh-cn/library/ms235631(VS.80).aspx]
从 char * 转换
示例
此示例演示如何从 char * 转换为上面列出的其他字符串类型。
![]() |
// convert_from_char.cpp// compile with: /clr /link comsuppw.lib #include <iostream>#include <stdlib.h>#include <string> #include "atlbase.h"#include "atlstr.h"#include "comutil.h" using namespace std;using namespace System; int main(){char *orig = "Hello, World!";cout << orig << " (char *)" << endl; // Convert to a wchar_t*size_t origsize = strlen(orig) + 1;const size_t newsize = 100;size_t convertedChars = 0;wchar_t wcstring[newsize];mbstowcs_s(&convertedChars, wcstring, origsize, orig, _TRUNCATE);wcscat_s(wcstring, L" (wchar_t *)");wcout << wcstring << endl; // Convert to a _bstr_t_bstr_t bstrt(orig);bstrt += " (_bstr_t)";cout << bstrt << endl; // Convert to a CComBSTRCComBSTR ccombstr(orig);if (ccombstr.Append(L" (CComBSTR)") == S_OK){CW2A printstr(ccombstr);cout << printstr << endl;} // Convert to a CStringCString cstring(orig);cstring += " (CString)";cout << cstring << endl; // Convert to a basic_stringstring basicstring(orig);basicstring += " (basic_string)";cout << basicstring << endl; // Convert to a System::StringString ^systemstring = gcnew String(orig);systemstring += " (System::String)";Console::WriteLine("{0}", systemstring);delete systemstring;} |
输出
Hello, World! (char *) Hello, World! (wchar_t *) Hello, World! (_bstr_t) Hello, World! (CComBSTR) Hello, World! (CString) Hello, World! (basic_string) Hello, World! (System::String)
从 wchar_t * 转换
示例
此示例演示如何从 wchar_t * 转换为上面列出的其他字符串类型。
![]() |
// convert_from_wchar_t.cpp// compile with: /clr /link comsuppw.lib #include <iostream>#include <stdlib.h>#include <string> #include "atlbase.h"#include "atlstr.h"#include "comutil.h" using namespace std;using namespace System; int main(){wchar_t *orig = L"Hello, World!";wcout << orig << L" (wchar_t *)" << endl; // Convert to a char*size_t origsize = wcslen(orig) + 1;const size_t newsize = 100;size_t convertedChars = 0;char nstring[newsize];wcstombs_s(&convertedChars, nstring, origsize, orig, _TRUNCATE);strcat_s(nstring, " (char *)");cout << nstring << endl; // Convert to a _bstr_t_bstr_t bstrt(orig);bstrt += " (_bstr_t)";cout << bstrt << endl; // Convert to a CComBSTRCComBSTR ccombstr(orig);if (ccombstr.Append(L" (CComBSTR)") == S_OK){CW2A printstr(ccombstr);cout << printstr << endl;} // Convert to a CStringCString cstring(orig);cstring += " (CString)";cout << cstring << endl; // Convert to a basic_stringwstring basicstring(orig);basicstring += L" (basic_string)";wcout << basicstring << endl; // Convert to a System::StringString ^systemstring = gcnew String(orig);systemstring += " (System::String)";Console::WriteLine("{0}", systemstring);delete systemstring;} |
输出
Hello, World! (wchar_t *) Hello, World! (char *) Hello, World! (_bstr_t) Hello, World! (CComBSTR) Hello, World! (CString) Hello, World! (basic_string) Hello, World! (System::String)
从 _bstr_t 转换
示例
此示例演示如何从 _bstr_t 转换为上面列出的其他字符串类型。
![]() |
// convert_from_bstr_t.cpp// compile with: /clr /link comsuppw.lib #include <iostream>#include <stdlib.h>#include <string> #include "atlbase.h"#include "atlstr.h"#include "comutil.h" using namespace std;using namespace System; int main(){_bstr_t orig("Hello, World!");wcout << orig << " (_bstr_t)" << endl; // Convert to a char*const size_t newsize = 100;char nstring[newsize];strcpy_s(nstring, (char *)orig);strcat_s(nstring, " (char *)");cout << nstring << endl; // Convert to a wchar_t*wchar_t wcstring[newsize];wcscpy_s(wcstring, (wchar_t *)orig);wcscat_s(wcstring, L" (wchar_t *)");wcout << wcstring << endl; // Convert to a CComBSTRCComBSTR ccombstr((char *)orig);if (ccombstr.Append(L" (CComBSTR)") == S_OK){CW2A printstr(ccombstr);cout << printstr << endl;} // Convert to a CStringCString cstring((char *)orig);cstring += " (CString)";cout << cstring << endl; // Convert to a basic_stringstring basicstring((char *)orig);basicstring += " (basic_string)";cout << basicstring << endl; // Convert to a System::StringString ^systemstring = gcnew String((char *)orig);systemstring += " (System::String)";Console::WriteLine("{0}", systemstring);delete systemstring;} |
输出
Hello, World! (_bstr_t) Hello, World! (char *) Hello, World! (wchar_t *) Hello, World! (CComBSTR) Hello, World! (CString) Hello, World! (basic_string) Hello, World! (System::String)
从 CComBSTR 转换
示例
此示例演示如何从 CComBSTR 转换为上面列出的其他字符串类型。
![]() |
// convert_from_ccombstr.cpp// compile with: /clr /link comsuppw.lib #include <iostream>#include <stdlib.h>#include <string> #include "atlbase.h"#include "atlstr.h"#include "comutil.h"#include "vcclr.h" using namespace std;using namespace System;using namespace System::Runtime::InteropServices; int main(){CComBSTR orig("Hello, World!");CW2A printstr(orig);cout << printstr << " (CComBSTR)" << endl; // Convert to a char*const size_t newsize = 100;char nstring[newsize];CW2A tmpstr1(orig);strcpy_s(nstring, tmpstr1);strcat_s(nstring, " (char *)");cout << nstring << endl; // Convert to a wchar_t*wchar_t wcstring[newsize];wcscpy_s(wcstring, orig);wcscat_s(wcstring, L" (wchar_t *)");wcout << wcstring << endl; // Convert to a _bstr_t_bstr_t bstrt(orig);bstrt += " (_bstr_t)";cout << bstrt << endl; // Convert to a CStringCString cstring(orig);cstring += " (CString)";cout << cstring << endl; // Convert to a basic_stringwstring basicstring(orig);basicstring += L" (basic_string)";wcout << basicstring << endl; // Convert to a System::StringString ^systemstring = gcnew String(orig);systemstring += " (System::String)";Console::WriteLine("{0}", systemstring);delete systemstring;} |
输出
Hello, World! (CComBSTR) Hello, World! (char *) Hello, World! (wchar_t *) Hello, World! (_bstr_t) Hello, World! (CString) Hello, World! (basic_string) Hello, World! (System::String)
从 CString 转换
示例
此示例演示如何从 CString 转换为上面列出的其他字符串类型。
![]() |
// convert_from_cstring.cpp// compile with: /clr /link comsuppw.lib #include <iostream>#include <stdlib.h>#include <string> #include "atlbase.h"#include "atlstr.h"#include "comutil.h" using namespace std;using namespace System; int main(){CString orig("Hello, World!");wcout << orig << " (CString)" << endl; // Convert to a char*const size_t newsize = 100;char nstring[newsize];strcpy_s(nstring, orig);strcat_s(nstring, " (char *)");cout << nstring << endl; // Convert to a wchar_t*// You must first convert to a char * for this to work.size_t origsize = strlen(orig) + 1;size_t convertedChars = 0;wchar_t wcstring[newsize];mbstowcs_s(&convertedChars, wcstring, origsize, orig, _TRUNCATE);wcscat_s(wcstring, L" (wchar_t *)");wcout << wcstring << endl; // Convert to a _bstr_t_bstr_t bstrt(orig);bstrt += " (_bstr_t)";cout << bstrt << endl; // Convert to a CComBSTRCComBSTR ccombstr(orig);if (ccombstr.Append(L" (CComBSTR)") == S_OK){CW2A printstr(ccombstr);cout << printstr << endl;} // Convert to a basic_stringstring basicstring(orig);basicstring += " (basic_string)";cout << basicstring << endl; // Convert to a System::StringString ^systemstring = gcnew String(orig);systemstring += " (System::String)";Console::WriteLine("{0}", systemstring);delete systemstring;} |
输出
Hello, World! (CString) Hello, World! (char *) Hello, World! (wchar_t *) Hello, World! (_bstr_t) Hello, World! (CComBSTR) Hello, World! (basic_string) Hello, World! (System::String)
从 basic_string 转换
示例
此示例演示如何从 basic_string 转换为上面列出的其他字符串类型。
![]() |
// convert_from_basic_string.cpp// compile with: /clr /link comsuppw.lib #include <iostream>#include <stdlib.h>#include <string> #include "atlbase.h"#include "atlstr.h"#include "comutil.h" using namespace std;using namespace System; int main(){string orig("Hello, World!");cout << orig << " (basic_string)" << endl; // Convert to a char*const size_t newsize = 100;char nstring[newsize];strcpy_s(nstring, orig.c_str());strcat_s(nstring, " (char *)");cout << nstring << endl; // Convert to a wchar_t*// You must first convert to a char * for this to work.size_t origsize = strlen(orig.c_str()) + 1;size_t convertedChars = 0;wchar_t wcstring[newsize];mbstowcs_s(&convertedChars, wcstring, origsize, orig.c_str(), _TRUNCATE);wcscat_s(wcstring, L" (wchar_t *)");wcout << wcstring << endl; // Convert to a _bstr_t_bstr_t bstrt(orig.c_str());bstrt += " (_bstr_t)";cout << bstrt << endl; // Convert to a CComBSTRCComBSTR ccombstr(orig.c_str());if (ccombstr.Append(L" (CComBSTR)") == S_OK){CW2A printstr(ccombstr);cout << printstr << endl;} // Convert to a CStringCString cstring(orig.c_str());cstring += " (CString)";cout << cstring << endl; // Convert to a System::StringString ^systemstring = gcnew String(orig.c_str());systemstring += " (System::String)";Console::WriteLine("{0}", systemstring);delete systemstring;} |
输出
Hello, World! (basic_string) Hello, World! (char *) Hello, World! (wchar_t *) Hello, World! (_bstr_t) Hello, World! (CComBSTR) Hello, World! (CString) Hello, World! (System::String)
从 System::String 转换
示例
此示例演示如何从 System.String 转换为上面列出的其他字符串类型。
![]() |
// convert_from_system_string.cpp// compile with: /clr /link comsuppw.lib #include <iostream>#include <stdlib.h>#include <string> #include "atlbase.h"#include "atlstr.h"#include "comutil.h"#include "vcclr.h" using namespace std;using namespace System;using namespace System::Runtime::InteropServices; int main(){String ^orig = gcnew String("Hello, World!");Console::WriteLine("{0} (System::String)", orig); pin_ptr<const wchar_t> wch = PtrToStringChars(orig); // Convert to a char*size_t origsize = wcslen(wch) + 1;const size_t newsize = 100;size_t convertedChars = 0;char nstring[newsize];wcstombs_s(&convertedChars, nstring, origsize, wch, _TRUNCATE);strcat_s(nstring, " (char *)");cout << nstring << endl; // Convert to a wchar_t*wchar_t wcstring[newsize];wcscpy_s(wcstring, wch);wcscat_s(wcstring, L" (wchar_t *)");wcout << wcstring << endl; // Convert to a _bstr_t_bstr_t bstrt(wch);bstrt += " (_bstr_t)";cout << bstrt << endl; // Convert to a CComBSTRCComBSTR ccombstr(wch);if (ccombstr.Append(L" (CComBSTR)") == S_OK){CW2A printstr(ccombstr);cout << printstr << endl;} // Convert to a CStringCString cstring(wch);cstring += " (CString)";cout << cstring << endl; // Convert to a basic_stringwstring basicstring(wch);basicstring += L" (basic_string)";wcout << basicstring << endl; delete orig;} |
输出
Hello, World! (System::String) Hello, World! (char *) Hello, World! (wchar_t *) Hello, World! (_bstr_t) Hello, World! (CComBSTR) Hello, World! (CString) Hello, World! (basic_string)
转载于:https://www.cnblogs.com/taoxu0903/archive/2008/10/31/1323860.html
如何:在各种字符串类型之间进行转换相关推荐
- strconv---用来基本类型之间的转换
strconv---用来基本类型之间的转换 字符串转int:Atoi(s string)(i int,err error) int转字符串: Itoa(i int)string string转换为对应 ...
- pythonDay5-基本语法-中文编码-多行语句连接-输入输出-个人名片制作-类型之间的转换-判断用户的年龄
python项目-Day5 pycharm快捷键 复制当前行ctrl+D 删除当前行ctrl+x 注释当前行ctrl+/ 多行注释"' "' 单行注释# 另起一行shift+ent ...
- javaScript基本数据类型与类型之间的转换
1.number类型(数值类型) number类型又分为整数类型(int),浮点型(float). 整数类型就是整数比如:1 ,30,90等 浮点数就是有小数的数比如:0.1,9.99,5.55555 ...
- 自学Python第二十二天- Django框架(三) AJAX、文件上传、POST 请求类型之间的转换、多APP开发、iframe、验证码、分页器、类视图、中间件、信号、日志、缓存、celery异步
Django官方文档 django 使用 AJAX django 项目中也可以使用 ajax 技术 前端 前端和其他 web 框架一样,需要注意的是,django 接收 POST 请求时,需要 csr ...
- 有两个地方,用到了javabean对象和属性字符串值之间的转换
1.有两个地方,用到了javabean对象和属性字符串值之间的转换 2.一个是接入层spring mvc,将json字符串参数转换为javaBean.通过@RequestBody javaBean方式 ...
- mysql time类型转换_mysql8 参考手册--Date日期和Time时间类型之间的转换
在某种程度上,您可以将值从一种时间类型转换为另一种时间类型.但是,价值可能会有所变化或信息丢失.在所有情况下,时间类型之间的转换都取决于结果类型的有效值范围.例如,尽管 DATE, DATETIME和 ...
- c语言 字符转int型,C语言—类型之间的转换
原标题:C语言-类型之间的转换 当混合不同类型的数据进行计算时,便会发生类型转换. 当不同类型的数据进行计算时,应首先将操作数转换成相同的数据类型,然后再进行计算. 类型转换有两种形式,即隐式类型转换 ...
- 26.JavaScript对象和基础类型之间的转换、hint、Symbol.toPrimitive、toString、valueOf
文章目录 对象-基础类型转换 写在前面 对象类型转换规则 对象类型转换的结果 Hint String Number Default 转换需要的三个对象方法 Symbol.toPrimitive() t ...
- 无法将 char 值转换为 money。该 char 值的语法有误。_Java类型之间的转换
今天我要说得是关于Java类型之间相互转换的事. 对于Java基本之间的转换来说,大致分为两种,分别是:自动类型转换和强制类型转换. 自动类型转换 所谓自动类型转换就是指两种类型转换时无需附加额外的操 ...
最新文章
- ubuntu下关于 undefined reference to 'pcap_flex'错误 以及 无法导入/找到libpcap.so.1错误...
- mysql数据库教程外联_MySQL--外联语句练习
- Servlet映射路径中的通配符
- #1176 : 欧拉路·一(欧拉通路的判定)
- Exp4 恶意代码分析 20164302 王一帆
- 《看聊天记录都学不会C#?太菜了吧》(5)C# 中可以用中文名变量?
- Python进阶丨如何创建你的第一个Python元类?
- C# abstract ,virtual ,override,new --比较好的文章
- 【板栗糖GIS】kmz数据是什么,如何打开,普通数据如何转换成kmz格式
- 2022年五一建模比赛A题#五一建模
- Linux FTP服务搭建(完整步骤)
- 生成小程序二维码(草料)
- Vitis指南 | Xilinx Vitis 系列(一)
- 【NOIP practice】BSOJ 3140 冲出亚洲 模拟
- 行为识别阅读笔记(paper + parted code):Beyond Frame-level CNN Saliency-Aware 3-D CNN with LSTM for Video Acti
- 友盟 推特分享错误
- 5个超好用的图片素材库,建议收藏~
- 倪光南院士 你该检讨一下了
- 总结:对象存储、块存储、文件存储的区别
- Comparator中compare方法的使用
热门文章
- java中scanner与hashmap_Java中HashMap的使用练习
- c语言printf %llo,c++ - Printf疯狂了 - 堆栈内存溢出
- 崇天老师python123测验6_嵩天老师python123测验1: Python基本语法元素 (第1周)
- python 获取向上两级路径_Python学习第171课--相对路径和绝对路径
- 结构体变量和结构体指针变量作为函数参数传递问题
- 产品经理面试全流程深度复盘【面试准备篇】
- 互联网晚报 | 9月22日 星期三 | 中国电信控股股东拟40亿元增持;碧桂园服务100亿收购富良环球;搜狗浏览器论坛即将下线...
- java 异步_聊聊java高并发系统之异步非阻塞
- 快手小店电脑版_快手抖音主播同款谷歌地球手机版+电脑版+使用教程(在家旅游神器)...
- 作者:夏帆(1988-),男,华东师范大学计算机科学与软件工程学院博士后。...