C++字符串与数值——安全的类型转换扩展

数值和字符的转换一直是C++的弱项,C++只提供来自C语言的单向不安全转换。
(C语言中,atoi函数和atof函数可以把字符串转换为数值,但没有反转函数)
如若要反转,只能用不安全的printf().
虽然很多编译器额外提供反向转换(如itoa,_itoa函数),但也是相当不安全。

因此我开发了一个可以安全的双向转换的库以备使用。

库文件下载:
http://pan.baidu.com/netdisk/singlepublic?fid=199923_3319019563
http://dl.dbank.com/c0eaq7aa8b
文件列表:
TYSafeCast.h
TYSafeCast10.dll
TYSafeCast10.lib
TYSafeCast10d.dll
TYSafeCast10d.lib
TYSafeCast.def
msvcp100.dll
msvcr100.dll
msvcp100d.dll
msvcr100d.dll

 1 /*- ==========================================================
 2 *     文件名  :TYSafeCast.h
 3 *     开发人员:袁培荣
 4 *     当前版本:1.0.0.2595
 5 *     创建时间:2012-05-31
 6 *     修改时间:2012-05-31
 7 *     功能说明:安全的类型转换扩展
 8 *     版权说明:版权所有 袁培荣 YuanPeirong 
 9 *     编译环境:Windows 7(x64) SP1 简体中文专业版
10 *     编译器:  Visual Studio 2010 SP1(中文旗舰版)
11                 MinGW 20120426 GNU GCC 4.6.2
12                 Visual C++ 6.0 SP6(中文企业版)
13 - ==========================================================*/
14 
15 #ifndef SafeCast_H_TYCppStdLib
16 #define SafeCast_H_TYCppStdLib
17 
18 #ifdef SafeCast_DLL_API
19 #else
20 #define SafeCast_DLL_API _declspec(dllimport)
21 #endif
22 
23 #include <string>
24 
25 using namespace std;
26 
27 namespace TYCppStdLib
28 {   
29     template<typename T>
30     class CCast
31     {
32     public:
33         T value; 
34         bool castOK;
35     };
36 
37     typedef CCast<int> CCastInt;
38     typedef CCast<long> CCastLong;
39     typedef CCast<float> CCastFloat;
40     typedef CCast<double> CCastDouble;
41     typedef CCast<string> CCastString;
42     
43     //================================================
44     // 以下16个函数用于字符串和数值的相互转换
45     // 字符串可以是:
46     // 字面值常量,char*, const char*, string,const string共五种
47     // 数值可以是: 
48     // int,long,float,double共四种(对于其他都可以先转为此四种之一)
49 
50     // 对于不带Ex后缀的版本,直接返回转换后的值,
51     // 若转换失败,对字符串,返回空字符串,对数值,返回0
52 
53     // 对于带Ex后缀的版本,返回相应类的对象
54     // 其value变量保存转换后的值
55     // 若转换失败,对字符串,value为空字符串,对数值,value为0
56     // 其castOK变量保存转换信息
57     // 若转换成功 castOK为true,反之为false
58 
59     SafeCast_DLL_API int SCastInt(const string &str);
60     SafeCast_DLL_API CCastInt SCastIntEx(const string &str);
61     
62     SafeCast_DLL_API long SCastLong(const string &str);
63     SafeCast_DLL_API CCastLong SCastLongEx(const string &str);
64     
65     SafeCast_DLL_API float SCastFloat(const string &str);
66     SafeCast_DLL_API CCastFloat SCastFloatEx(const string &str);
67     
68     SafeCast_DLL_API double SCastDouble(const string &str);
69     SafeCast_DLL_API CCastDouble SCastDoubleEx(const string &str);
70     
71     SafeCast_DLL_API string ICastString(int n);
72     SafeCast_DLL_API CCastString ICastStringEx(int n);
73     SafeCast_DLL_API string LCastString(long n);
74     SafeCast_DLL_API CCastString LCastStringEx(long n);
75     SafeCast_DLL_API string FCastString(float n);
76     SafeCast_DLL_API CCastString FCastStringEx(float n);
77     SafeCast_DLL_API string DCastString(double n);
78     SafeCast_DLL_API CCastString DCastStringEx(double n);
79     //================================================
80 
81 }
82 
83 #endif
  1 /*- ==========================================================
  2 *     文件名  :TestTYSafeCast.cpp
  3 *     开发人员:袁培荣
  4 *     当前版本:1.0.0.2595
  5 *     创建时间:2012-05-31
  6 *     修改时间:2012-05-31
  7 *     功能说明:安全的类型转换扩展 使用示例
  8 *     版权说明:版权所有 袁培荣 YuanPeirong 
  9 *     编译环境:Windows 7(x64) SP1 简体中文专业版
 10 *     编译器:  Visual Studio 2010 SP1(中文旗舰版)
 11                 MinGW 20120426 GNU GCC 4.6.2
 12                 Visual C++ 6.0 SP6(中文企业版)
 13 - ==========================================================*/
 14 
 15 #include <iostream>
 16 #include "TYSafeCast.h"                   
 17 #pragma comment(lib, "TYSafeCast10.lib")
 18 //#include <string>   //已经包含在SafeCast.h中
 19 
 20 using namespace std;
 21 using namespace TYCppStdLib;
 22 
 23 //在Debug模式下,请确保系统有msvcp100d.dll和msvcr100d.dll两个运行库
 24 //在Release模式下,请确保系统有msvcp100.dll和msvcr100.dll两个运行库
 25 
 26 //例如在Release模式下,本示例编译成功可执行文件TestTYSafeCast.exe
 27 //那么,此文件如要运行,就必须在同目录下有如下三个文件:
 28 // TYSafeCast10.dll msvcp100.dll msvcr100.dll
 29 //或者
 30 
 31 int main(int argc, char* argv[])
 32 {    
 33     string str="123";
 34     char *pch=static_cast<char*>("321");
 35     const string str2="123";
 36     const char *pch2="321";
 37     
 38     cout<<"全面测试FCastInt"<<endl;
 39     cout<<SCastInt("500")<<endl;
 40     cout<<SCastInt(str)<<endl;
 41     cout<<SCastInt(pch)<<endl;
 42     cout<<SCastInt(str2)<<endl;
 43     cout<<SCastInt(pch2)<<endl;
 44     
 45     cout<<"全面测试FCastIntEx"<<endl;
 46     cout<<"对str=\"123\""<<endl;
 47     CCastInt mycast;
 48     mycast=SCastIntEx(str);
 49     if(mycast.castOK)
 50         cout<<"str转换成功:"<<mycast.value<<endl;
 51     else
 52         cout<<"str转换失败:"<<mycast.value<<endl;
 53     
 54     str="123A";
 55     cout<<"对str=\"123A\""<<endl;
 56     mycast=SCastIntEx(str);
 57     if(mycast.castOK)
 58         cout<<"str转换成功:"<<mycast.value<<endl;
 59     else
 60         cout<<"str转换失败:"<<mycast.value<<endl;
 61     
 62     string si="100";
 63     string sl="200000";
 64     string sf="3.14";
 65     string sd="3.1415926";
 66     
 67     cout<<"测试数值变量转string(无Ex)"<<endl;
 68     cout<<SCastInt(si)<<endl;
 69     cout<<SCastLong(sl)<<endl;
 70     cout<<SCastFloat(sf)<<endl;
 71     cout<<SCastDouble(sd)<<endl;
 72     
 73     cout<<"测试数值常量转string(无Ex)"<<endl;
 74     cout<<SCastInt("100")<<endl;
 75     cout<<SCastLong("200000")<<endl;
 76     cout<<SCastFloat("3.14")<<endl;
 77     cout<<SCastDouble("3.1415926")<<endl;
 78     
 79     cout<<"测试数值变量转string(有Ex)"<<endl;
 80     cout<<SCastIntEx(si).value<<endl;
 81     cout<<SCastLongEx(sl).value<<endl;
 82     cout<<SCastFloatEx(sf).value<<endl;
 83     cout<<SCastDoubleEx(sd).value<<endl;
 84     
 85     cout<<"测试数值常量转string(有Ex)"<<endl;
 86     cout<<SCastIntEx("100").value<<endl;
 87     cout<<SCastLongEx("200000").value<<endl;
 88     cout<<SCastFloatEx("3.14").value<<endl;
 89     cout<<SCastDoubleEx("3.1415926").value<<endl;
 90     
 91     int i=100;
 92     long l=200000;
 93     float f=3.14;
 94     double d=3.1415926;
 95     
 96     cout<<"测试string转数值变量(无Ex)"<<endl;
 97     cout<<ICastString(i)<<endl;
 98     cout<<LCastString(l)<<endl;
 99     cout<<FCastString(f)<<endl;
100     cout<<DCastString(d)<<endl;    
101     
102     cout<<"测试string转数值常量(无Ex)"<<endl;
103     cout<<ICastString(100)<<endl;
104     cout<<LCastString(200000)<<endl;
105     cout<<FCastString(3.14)<<endl;
106     cout<<DCastString(3.1415926)<<endl;    
107     
108     cout<<"测试string转数值变量(有Ex)"<<endl;
109     cout<<ICastStringEx(i).value<<endl;
110     cout<<LCastStringEx(l).value<<endl;
111     cout<<FCastStringEx(f).value<<endl;
112     cout<<DCastStringEx(d).value<<endl;    
113     
114     cout<<"测试string转数值常量(有Ex)"<<endl;
115     cout<<ICastStringEx(100).value<<endl;
116     cout<<LCastStringEx(200000).value<<endl;
117     cout<<FCastStringEx(3.14).value<<endl;
118     cout<<DCastStringEx(3.1415926).value<<endl;    
119     
120     return 0;
121 }
122 
123 //=============
124 //运行结果:
125 //=============
126 // 全面测试FCastInt
127 // 500
128 // 123
129 // 321
130 // 123
131 // 321
132 // 全面测试FCastIntEx
133 // 对str="123"
134 // str转换成功:123
135 // 对str="123A"
136 // str转换失败:0
137 // 测试数值变量转string(无Ex)
138 // 100
139 // 200000
140 // 3.14
141 // 3.14159
142 // 测试数值常量转string(无Ex)
143 // 100
144 // 200000
145 // 3.14
146 // 3.14159
147 // 测试数值变量转string(有Ex)
148 // 100
149 // 200000
150 // 3.14
151 // 3.14159
152 // 测试数值常量转string(有Ex)
153 // 100
154 // 200000
155 // 3.14
156 // 3.14159
157 // 测试string转数值变量(无Ex)
158 // 100
159 // 200000
160 // 3.1400001
161 // 3.1415926000000001
162 // 测试string转数值常量(无Ex)
163 // 100
164 // 200000
165 // 3.1400000000000001
166 // 3.1415926000000001
167 // 测试string转数值变量(有Ex)
168 // 100
169 // 200000
170 // 3.1400001
171 // 3.1415926000000001
172 // 测试string转数值常量(有Ex)
173 // 100
174 // 200000
175 // 3.1400000000000001
176 // 3.1415926000000001
177 //=============

 

posted @ 2012-05-31 23:23  天远  阅读(572)  评论(0)    收藏  举报

版权所有 © 2010-2020 YuanPeirong TianYuan All Rights Reserved. Powered By 天远