字符串转换 - 应该熟练掌握的东西
一、其它数据类型转换为字符串
- 短整型(int)
itoa(i,temp,10);///将i转换为字符串放入temp中,最后一个数字表示十进制
itoa(i,temp,2); ///按二进制方式转换 - 长整型(long)
ltoa(l,temp,10); - 浮点数(float,double)
用fcvt可以完成转换,这是MSDN中的例子:
int decimal, sign;
char *buffer;
double source = 3.1415926535;
buffer = _fcvt( source, 7, &decimal, &sign );
运行结果:source: 3.1415926535 buffer: '31415927' decimal: 1 sign: 0
decimal表示小数点的位置,sign表示符号:0为正数,1为负数 - CString变量
str = "2008北京奥运";
buf = (LPSTR)(LPCTSTR)str; - BSTR变量
BSTR bstrValue = ::SysAllocString(L"程序员");
char * buf = _com_util::ConvertBSTRToString(bstrValue);
SysFreeString(bstrValue);
AfxMessageBox(buf);
delete(buf); - CComBSTR变量
CComBSTR bstrVar("test");
char *buf = _com_util::ConvertBSTRToString(bstrVar.m_str);
AfxMessageBox(buf);
delete(buf); - _bstr_t变量
_bstr_t类型是对BSTR的封装,因为已经重载了=操作符,所以很容易使用
_bstr_t bstrVar("test");
const char *buf = bstrVar;///不要修改buf中的内容
AfxMessageBox(buf); - 通用方法(针对非COM数据类型)
用sprintf完成转换
char buffer[200]; char c = '1'; int i = 35; long j = 1000; float f = 1.7320534f; sprintf( buffer, "%c",c); sprintf( buffer, "%d",i); sprintf( buffer, "%d",j); sprintf( buffer, "%f",f); 二、字符串转换为其它数据类型
strcpy(temp,"123"); - 短整型(int)
i = atoi(temp); - 长整型(long) l = atol(temp);
- 浮点(double) d = atof(temp);
- CString变量 CString name = temp;
- BSTR变量 BSTR bstrValue = ::SysAllocString(L"程序员");
...///完成对bstrValue的使用 SysFreeString(bstrValue);
- CComBSTR变量 CComBSTR类型变量可以直接赋值 CComBSTR bstrVar1("test"); CComBSTR bstrVar2(temp);
- _bstr_t变量 _bstr_t类型的变量可以直接赋值 _bstr_t bstrVar1("test");
_bstr_t bstrVar2(temp);
三、其它数据类型转换到CString使用CString的成员函数Format来转换,例如:
- 整数(int) str.Format("%d",i);
- 浮点数(float) str.Format("%f",i);
- 字符串指针(char *)等已经被CString构造函数支持的数据类型可以直接赋值 str = username;
- 对于Format所不支持的数据类型,可以通过上面所说的关于其它数据类型转化到char *的方法先转到char *,然后赋值给CString变量。
四、BSTR、_bstr_t与CComBSTR
- CComBSTR 是ATL对BSTR的封装,_bstr_t是C++对BSTR的封装,BSTR是32位指针,但并不直直指向字串的缓冲区。 char *转换到BSTR可以这样: BSTR b=_com_util::ConvertStringToBSTR("数据");///使用前需要加上comutil.h和comsupp.lib SysFreeString(bstrValue); 反之可以使用 char *p=_com_util::ConvertBSTRToString(b); delete p; 具体可以参考一,二段落里的具体说明。
CComBSTR与_bstr_t对大量的操作符进行了重载,可以直接进行=,!=,==等操作,所以使用非常方便。
特别是_bstr_t,建议大家使用它。
五、VARIANT 、_variant_t 与 COleVariant
VARIANT的结构可以参考头文件VC98/Include/OAIDL.H中关于结构体tagVARIANT的定义。
对于VARIANT变量的赋值:首先给vt成员赋值,指明数据类型,再对联合结构中相同数据类型的变量赋值,举个例子:
VARIANT va;
int a=2001;
va.vt=VT_I4;///指明整型数据
va.lVal=a; ///赋值
_variant_t是VARIANT的封装类,其赋值可以使用强制类型转换,其构造函数会自动处理这些数据类型。
使用时需加上#include <comdef.h>
例如:
long l=222;
ing i=100;
_variant_t lVal(l);
lVal = (long)i;
COleVariant的使用与_variant_t的方法基本一样,请参考如下例子:
COleVariant v3 = "字符串", v4 = (long)1999;
CString str =(BSTR)v3.pbstrVal;
long i = v4.lVal;
本主题演示如何将各种 C++ 字符串类型转换为其他字符串。以转换的字符串类型包括 char *、wchar_t*、_bstr_t、CComBSTR、CString、basic_string 和 System.String。在所有情况下,在将字符串转换为新类型时,都会创建字符串的副本。对新字符串进行的任何更改都不会影响原始字符串,反之亦然。
从 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 CComBSTR
CComBSTR ccombstr(orig);
if (ccombstr.Append(L" (CComBSTR)") == S_OK)
{
CW2A printstr(ccombstr);
cout << printstr << endl;
}
// Convert to a CString
CString cstring(orig);
cstring += " (CString)";
cout << cstring << endl;
// Convert to a basic_string
string basicstring(orig);
basicstring += " (basic_string)";
cout << basicstring << endl;
// Convert to a System::String
String ^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 * 转换
// 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 CComBSTR
CComBSTR ccombstr(orig);
if (ccombstr.Append(L" (CComBSTR)") == S_OK)
{
CW2A printstr(ccombstr);
cout << printstr << endl;
}
// Convert to a CString
CString cstring(orig);
cstring += " (CString)";
cout << cstring << endl;
// Convert to a basic_string
wstring basicstring(orig);
basicstring += L" (basic_string)";
wcout << basicstring << endl;
// Convert to a System::String
String ^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 转换// 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 CComBSTR
CComBSTR ccombstr((char *)orig);
if (ccombstr.Append(L" (CComBSTR)") == S_OK)
{
CW2A printstr(ccombstr);
cout << printstr << endl;
} // Convert to a CString
CString cstring((char *)orig);
cstring += " (CString)";
cout << cstring << endl; // Convert to a basic_string
string basicstring((char *)orig);
basicstring += " (basic_string)";
cout << basicstring << endl; // Convert to a System::String
String ^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 转换
// 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 CString
CString cstring(orig);
cstring += " (CString)";
cout << cstring << endl;// Convert to a basic_string
wstring basicstring(orig);
basicstring += L" (basic_string)";
wcout << basicstring << endl;// Convert to a System::String
String ^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 转换
// 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 CComBSTR
CComBSTR ccombstr(orig);
if (ccombstr.Append(L" (CComBSTR)") == S_OK)
{
CW2A printstr(ccombstr);
cout << printstr << endl;
}// Convert to a basic_string
string basicstring(orig);
basicstring += " (basic_string)";
cout << basicstring << endl;// Convert to a System::String
String ^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 转换
#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 CComBSTR
CComBSTR ccombstr(orig.c_str());
if (ccombstr.Append(L" (CComBSTR)") == S_OK)
{
CW2A printstr(ccombstr);
cout << printstr << endl;
}// Convert to a CString
CString cstring(orig.c_str());
cstring += " (CString)";
cout << cstring << endl;// Convert to a System::String
String ^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 转换// 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 CComBSTR
CComBSTR ccombstr(wch);
if (ccombstr.Append(L" (CComBSTR)") == S_OK)
{
CW2A printstr(ccombstr);
cout << printstr << endl;
}// Convert to a CString
CString cstring(wch);
cstring += " (CString)";
cout << cstring << endl;// Convert to a basic_string
wstring 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)源www.cnblogs.com/zhucl1006/archive/2008/01/04/1025959.html
字符串转换 - 应该熟练掌握的东西相关推荐
- oracle 转化为整数,字符串转换成整数——从源码学习
字符串转换成整数:输入一个表示整数的字符串,把该字符串转换成整数并输出,例如输入字符串"345",则输出整数345. 在笔试面试中,atoi 即「字符串转换成整数」是一个经典问题了 ...
- java array to string_数组到字符串转换(Array to string conversion)
数组到字符串转换(Array to string conversion) 这段代码有什么问题? 我不明白. 这是错误代码: 注意:第12行的C:\ xampp \ htdocs \ stage \ r ...
- 【Java】 剑指offer(67) 把字符串转换成整数
本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集 题目 请你写一个函数StrToInt,实现把字符串转换成整数这个功能 ...
- php函数用竖线隔开,用指定的分隔符号把字符串转换成数组《 PHP 基础 》
现在我这里有一个字符串 ... 就是这个 $tracks ... 它的值是用竖线分隔开的一些文字 ... 使用 explode 函数 ... 我们可以使用竖线作为分隔符,把这个字符串转换成一个数组. ...
- atoi java,leetcode题目8: 字符串转换整数 (atoi)(java)
题目描述 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 示例 输入: "42" 输出: 42 输入: " -42" 输出: -42 解释: 第一个 ...
- 微信小程序之js 字符串转换成数字的三种方法, 取float型小数点后两位数的方法
在js读取文本框或者其它表单数据的时候获得的值是字符串类型的,例如两个文本框a和b,如果获得a的value值为11,b的value值为9 ,那么a.value要小于b.value,因为他们都是字符串形 ...
- C# 数字转16进制和字符串转换16进制
数字转换16进制.前提是你输入的必须是数字,而不是别的东西. private string StringToHexString(string s)//数字转换16进制{long Num = Conve ...
- java字符串转换成字节数组_将Java字符串转换为字节数组
我有一个要加密的字节数组,然后转换为字符串,以便可以传输. 当我收到字符串时,我必须将字符串转换回字节数组,以便可以对其进行解密. 我检查了接收到的字符串是否与发送的字符串(包括长度)匹配,但是当我使 ...
- python字符串转换成数字_python 字符与数字如何转换
展开全部 一.python中字符串转换成数字 1.类中进行导入:import string ,str='555',num=string.atoi(str),num即为str转换成的数字转换为浮点数:s ...
最新文章
- ARKIT/ARCore对比分析(二)
- 微软华人团队刷新COCO记录!全新目标检测机制达到SOTA|CVPR 2021
- 软件设计原则——里氏代换原则
- Ubuntu 16.04下的LAMP环境配置
- PyCharm中配置与PyQT5相关的External tools
- 【ElasticSearch】Es 源码之 PersistedClusterStateService 源码解读
- vue----sourceMap
- python怎么爬取知乎回答并制作词云_爬虫|爬取微博动态
- double im2double mat2gray之一二说
- Can't connect to MySQL server on 'localhost' (10038)
- 提高工作效率的几个网站
- 怎么在计算机修复flash,请修复adobe flash player 提示反复出现的最佳解决方法
- 第十三章:相关方管理
- Hive学习之路 (二)Hive安装
- 新加坡内推 | Motional新加坡分部招聘自动驾驶感知算法实习生
- day11-单点登录系统
- 干货分享|被PubMed收录的论文,在MEDLINE和SCIE能检索到吗?
- oracle 00600 kccpb,【案例】Oracle报错ORA-00600 kccpb_sanity_check_2 恢复控制文件过程
- 导入semantic-ui-css/semantic.min.css样式失败
- 【技术总结】Python常见技巧