您的位置:首页 > 编程语言 > C语言/C++

[Tip: _itow]数值和字符串相互转换(C++ 数据类型转换技巧)

2010-10-15 16:26 573 查看
转载自:C++ 数据类型转换技巧

类型转换是将一种类型的值映射为另一种类型的值。进行数据类型的转换。
是在实际代码编写中经常遇到的问题,特别是字符串和其他类型的转换。

1.将字符串转换为整数

(1).转换函数
// 双精度函数
double atof(
const char *string
);
double _wtof(
const wchar_t *string
);
自适应
TCHAR: _tstof 、 _ttof
VS2005:_atof_l 、_wtof_l 、 _atodbl 、 _atodbl_l

// 整型函数
int atoi(
const char *string
);
_int64 _atoi64(
const char *string
);
int _wtoi(
const wchar_t *string
);
_int64 _ wtoi64(
const char *string
);
自适应
TCHAR:_tstoi 、 _ttoi 、_tstoi64 、_ttoi64
VS2005:_atoi_l 、 _wtoi_l 、_atoi64_l 、_wtoi64_l

//长整形函数
long atol(
const char * string
);
long _wtol(
const wchar_t *string
);
自适应
TCHAR:_tstoi 、_ttoi
VS2005:_atoi_l 、_wtoi_l

可参考:http://blog.sina.com.cn/s/blog_4135af570100b0d9.html

(2).代码参考
Cstring ting ="1234";
int num = atoi(ting);
num的值为1234.
CString ting = "1234 ";
double num = atof(ting);
最终结果:num值为 1234.0000000000000

2.int 转换为字符串
cahr *_itoa(
int value,
char *string,
int radix
);
char *_i64toa(
_int64 value,
char *string,
int radix
);
char * _ui64toa(
unsigned _int64 value,
char *string,
int radix
);
wchar_t * _itow(
int value,
wchar_t *string,
int radix
);
wchar_t * _i64tow(
_int64 value,
wchar_t *string,
int radix
);
wchar_t * _ui64tow(
unsigned _int64 value,
wchar_t *string,
int radix
);
参数的意义:value 是指要转换的整数,sring 是用来存放转换后结果的便利,
radix是用来说明转换成几进制的数据,默认值是十进制数的。
转换的进制范围是二进制到三十六进制。 示例代码:
int iii = 123456789;
char ii[100];
itoa(iii, ii, 10);
ii 中的结果就是字符串"123456789"
int iii = 12;
char ii[100];
itoa(iii, ii, 2);
ii 中的结果是字符串"1100"。

3.long 转换为字符串

char *_ltoa( long value,char *string, int radix );
wchar_t *_ltow( long value, wchar_t *string, int radix );
其中,参数 value 为被转换的值,参数string为字符串缓冲区,radix为进制。
代码参考:
ling l = 100; char temp[10];
memset(temp,0,10);
ltoa(l,temp,10);

4.double 转换为字符串

char *_fcvt( double value, int count, int *dec, int *sign );
其中参数value 为双精度数,参数count为转换的小数点后面的位数,dec表示小数点的位置,
sign 表示符号。代码参数如下
int decimal, sign;
char *buffer;
dobule source = 3.1415926535;
buffer = _fcbt( source, 7, &decimal, &sign );
输出结果:sourec:3.1415926535; buffer:'31415927' decimal: 1 sign:0

5.日期类型转换为字符串

将一日期格式转换为字符串,利用了格式化函数,参考代码如下:
CTime ti = Ctime::GetCurrentTime();
Cstring strTemp("");
strTemp = ti.Format("%Y%m%d %H%H%S")

6.字符串转换为短整型

int atoi( const char *string );
其中,参数 const char *string为要转换的字符串,返回值为转换的结果。

7.字符串转换为长整型

long atol(const char * string)

8.字符串转换为双精度类型

double atof(const char* string)
代码参考:
#include<stdlib.h>
#include<stdio.h>

void main( void )
{
char *s; dobule x; int i; long l;

s = " -2309.12E-15"; // 测试atof
x = atof(s);
printf( "atof test:ASCII string: %s\tfloat: %e\n", s , x );

s= "7.8912654773d210"; // 测试atof
x = atof(s);
printf( "atof test: ASCII string: %s\tfloat: %e\n", s, x);

s = " -9885 pigs"; // 测试atoi
i = atoi(s);
printf( "atoi test:ASCII string:%s\t\tinteger: %d\n", s, i);

s = "98854 dollars"; // 测试atol
l = atol(s);
printf( "atoi test:ASCII string:%s\t\tlong: %ld\n", s, l);
}
输出结果:
atof test:ASCII string: -2309.12E-15 float:-2.309120e-012
atof test:ASCII string: 7.8912654773d210 float:7.891265E+210
atof test:ASCII string: -9885 pigs integer:-9885
atof test:ASCII string: 98854 dollars long:98854

9. char*和CString的相互转换

CString 饱含了3个值:指向某个数据缓冲区的指针、该缓冲区中有效的字符记数
(它是不可存取的,是位于CString 地址之下的一个隐藏区域)及一个缓冲区长度。
有效字符数的大小可以是从0到该缓冲最大长度值减1之间的任何数(因为字符串结尾有一个NULL字符)。
(1) char* 转换为 CString
☆直接赋值
☆利用格式化转换
CString strConvert;
TCHAR* P = _T("this is a chTostring test ");
strConvert = p; // 直接赋值
strConvert.Format("%s",p); // 格式化
(2) CString 转换为 char*
☆强制类型转换为LPCTSTR
CString theString((_T("Char test "));
LPTSTR lpsz = (LPTSTR) (LPCTSTR)theString;
☆使用strcpy
CString theString( (_T("Char test "));
LPTSTR lpsz = new TCHAR[theString.GetLength()+1];
_tcscopy(lpsz, theSting);
需要说明的是, strcpy(或可移植的_tcscpy)的第二参数是const wcahr_t*(Unicode)
或const char*(ANSI),系统编译器将会自动对其进行转换。
(3)使用GetBuffer
如果需要修改CString中的内容,它有一个特殊的方法可以使用,哪就是GetBuffer,
它的作用是返回一个可写的缓冲指针。如果只是打算修改字符或者截短字符串,
例如:
CString s(_T("Char test "));
LPTSTR p = s.GetBuffer();
// 添加P的代码
s.ReleaseBuffer() // 使用完后及时释放。

10.CString与BSTR的相互转换

(1).CString 转换为 BSTR
调用CString 对象的AllocSysString 方法将CString转化成BSTR,如:
CString str = "this is a test ";
BSTR b = str.AllosysString();
str.FreeSystring(); // 最后不要忘记了释放
(2).BSTR转换为CString
CString convert(BSTR b)
{
CString s;
if(b == NULL)
return s;
#ifdef UNICODE
s = b;
#else
LPSTR p = s.GetBuffer(SYsStringLen(b) + 1);
::WidecharToMultiByte(CP_ACP,
0,
b,
-1,
p,
SysStringLen(b)+1,
NULL,
NULL);
s.ReleaseBuffer();
#endif
return s;
}

11. BSTR、_bstr_t 与 CComBSTR的关系

CComBSTR是ATL对BSTR的封装,_bstr_t是C++对BSTR的封装,
BSTR是32位指针,但并不是直接指向字符串的缓冲区。
(1). char *转换到BSTR
BSTR b = _com_util::ConvertStringToBSTR("数据");
SysFreeString(bstrValue);
(2).BSTR转换到char*
char *p=_com_util::ConvertBSTRToString(b);
delete p;

12.typedef和typename关键字

这两个关键字在运用的时候时常会令人迷惑,其实很简单,typedef是类型定义,
而typename是类型解释。
(1).typedef关键字
类型说明给已经存在的数据类型起一个别名,定义名字表示这个类型的新名字。
类型说明的格式为typedef 类型 定义名;
例如,用下面语句定义整型数的新名字:
typedef int INT_COUNT; // 可读性更强(计数器类型为整型)
INT_COUNT intcount;
intCount = 123;
cout << intCount << endl;
typedef同样可用来说明结构、联合以及枚举和类。 如:
typedef struct book{
float fprice;
char* szAuthor[20];
int nPage;}
*BOOK;
viod fun()
{
BOOK mybook = new book;
myBook -> fPrice = 40.80;
myBook -> szAuthor = "jf";
myBook -> npage = 200;
}
(2).typename 关键字
typename和class用在模版定义开始部分时,它们的意义相同,但tpyename是用来
告诉编译器模版中有一个表示类型名。
Template<clsaa T>calss fun{
Void bar(){
T::Bletch * p;
.... ....
}
}
在上述代码中,T::Bletch * p是指什么,这一点就很让人迷惑。但如果写成这样就不会
出现这个问题:
Template<class T> class fun
{
Void bar()
{
Typename T::Bletch * p;
}
}
在模版定义里,也可以用<typename T>代替<class T>,即 template<class T>与
templare<typename T>具有相同意义。

13.VARIANT的处理技巧

1.VARIANT的定义
VARIANT被称为变体类型,其定义可以参考头文件VC98\INCLUDE\OAIDL.H
关于结构体 tagVARIANT定义。

2.引入VARIANT的原因
目前计算机语言多种多样,如C++、java、Basic、Pascal等,它们各自又都有自己的
数据类型,VARIANT数据类型就具有跨语言的特性,同时它可以表示(储存)任意类型
的数据。从C语言的角度来讲,VARIANT其实是一个结构,结构中用一个域(vt)表示,同时
正真的数据则存储在union空间中。

3.VARIANT的应用
利用VARIANT表示可一个整型数据:
VARIANT va;
int a=2001;
va.vt = VT_I4; // 指明整型数据
va.lVal=a; // 赋值
利用VARIANT表示一个布尔值:
VARIANT va;
vb.vt = VT_BOOL; // 指明整型数据
va.boolVal = VARIANT_TRUE; // 赋值
利用VARIANT保存一个字符串:
VARIANT va;
va.vt = VT_BSTR;
va.bstrVal = SysAllocString(L"Hello,this is a test ");
从上面可以看出这种类型使用起来比较复杂,其实有简单的方法,采用VARIANT的封装类_variant_t,其赋值
可以使用强制类型转换,其构造函数会自动处理这些数据类型。
代码参考如下:
long l=222;
ing i=100;
_varint_t lVal(l);
lVal = (long)i;
也可以用ColeVariant来简化对VARIANT的操作,代码参考如下:
COleVariant v3 = "this is a test", v4 = (long)1999;
CString str = (BSTR)v3.pbstrVal;
long i = v4.lVal;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: