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

11 篇文章 0 订阅

数值和字符的转换一直是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

/*- ==========================================================
*     文件名  :TYSafeCast.h
*     开发人员:袁培荣
*     当前版本:1.0.0.2595
*     创建时间:2012-05-31
*     修改时间:2012-05-31
*     功能说明:安全的类型转换扩展
*     版权说明:版权所有 袁培荣 YuanPeirong 
*     编译环境:Windows 7(x64) SP1 简体中文专业版
*     编译器:  Visual Studio 2010 SP1(中文旗舰版)
                MinGW 20120426 GNU GCC 4.6.2
                Visual C++ 6.0 SP6(中文企业版)
- ==========================================================*/

#ifndef SafeCast_H_TYCppStdLib
#define SafeCast_H_TYCppStdLib

#ifdef SafeCast_DLL_API
#else
#define SafeCast_DLL_API _declspec(dllimport)
#endif

#include <string>

using namespace std;

namespace TYCppStdLib
{   
    template<typename T>
    class CCast
    {
    public:
        T value; 
        bool castOK;
    };

    typedef CCast<int> CCastInt;
    typedef CCast<long> CCastLong;
    typedef CCast<float> CCastFloat;
    typedef CCast<double> CCastDouble;
    typedef CCast<string> CCastString;
    
    //================================================
    // 以下16个函数用于字符串和数值的相互转换
    // 字符串可以是:
    // 字面值常量,char*, const char*, string,const string共五种
    // 数值可以是: 
    // int,long,float,double共四种(对于其他都可以先转为此四种之一)

    // 对于不带Ex后缀的版本,直接返回转换后的值,
    // 若转换失败,对字符串,返回空字符串,对数值,返回0

    // 对于带Ex后缀的版本,返回相应类的对象
    // 其value变量保存转换后的值
    // 若转换失败,对字符串,value为空字符串,对数值,value为0
    // 其castOK变量保存转换信息
    // 若转换成功 castOK为true,反之为false

    SafeCast_DLL_API int SCastInt(const string &str);
    SafeCast_DLL_API CCastInt SCastIntEx(const string &str);
    
    SafeCast_DLL_API long SCastLong(const string &str);
    SafeCast_DLL_API CCastLong SCastLongEx(const string &str);
    
    SafeCast_DLL_API float SCastFloat(const string &str);
    SafeCast_DLL_API CCastFloat SCastFloatEx(const string &str);
    
    SafeCast_DLL_API double SCastDouble(const string &str);
    SafeCast_DLL_API CCastDouble SCastDoubleEx(const string &str);
    
    SafeCast_DLL_API string ICastString(int n);
    SafeCast_DLL_API CCastString ICastStringEx(int n);
    SafeCast_DLL_API string LCastString(long n);
    SafeCast_DLL_API CCastString LCastStringEx(long n);
    SafeCast_DLL_API string FCastString(float n);
    SafeCast_DLL_API CCastString FCastStringEx(float n);
    SafeCast_DLL_API string DCastString(double n);
    SafeCast_DLL_API CCastString DCastStringEx(double n);
    //================================================

}

#endif


 

/*- ==========================================================
*     文件名  :TestTYSafeCast.cpp
*     开发人员:袁培荣
*     当前版本:1.0.0.2595
*     创建时间:2012-05-31
*     修改时间:2012-05-31
*     功能说明:安全的类型转换扩展 使用示例
*     版权说明:版权所有 袁培荣 YuanPeirong 
*     编译环境:Windows 7(x64) SP1 简体中文专业版
*     编译器:  Visual Studio 2010 SP1(中文旗舰版)
                MinGW 20120426 GNU GCC 4.6.2
                Visual C++ 6.0 SP6(中文企业版)
- ==========================================================*/

#include <iostream>
#include "TYSafeCast.h"                   
#pragma comment(lib, "TYSafeCast10.lib")
//#include <string>   //已经包含在SafeCast.h中

using namespace std;
using namespace TYCppStdLib;

//在Debug模式下,请确保系统有msvcp100d.dll和msvcr100d.dll两个运行库
//在Release模式下,请确保系统有msvcp100.dll和msvcr100.dll两个运行库

//例如在Release模式下,本示例编译成功可执行文件TestTYSafeCast.exe
//那么,此文件如要运行,就必须在同目录下有如下三个文件:
// TYSafeCast10.dll msvcp100.dll msvcr100.dll
//或者

int main(int argc, char* argv[])
{    
    string str="123";
    char *pch=static_cast<char*>("321");
    const string str2="123";
    const char *pch2="321";
    
    cout<<"全面测试FCastInt"<<endl;
    cout<<SCastInt("500")<<endl;
    cout<<SCastInt(str)<<endl;
    cout<<SCastInt(pch)<<endl;
    cout<<SCastInt(str2)<<endl;
    cout<<SCastInt(pch2)<<endl;
    
    cout<<"全面测试FCastIntEx"<<endl;
    cout<<"对str=\"123\""<<endl;
    CCastInt mycast;
    mycast=SCastIntEx(str);
    if(mycast.castOK)
        cout<<"str转换成功:"<<mycast.value<<endl;
    else
        cout<<"str转换失败:"<<mycast.value<<endl;
    
    str="123A";
    cout<<"对str=\"123A\""<<endl;
    mycast=SCastIntEx(str);
    if(mycast.castOK)
        cout<<"str转换成功:"<<mycast.value<<endl;
    else
        cout<<"str转换失败:"<<mycast.value<<endl;
    
    string si="100";
    string sl="200000";
    string sf="3.14";
    string sd="3.1415926";
    
    cout<<"测试数值变量转string(无Ex)"<<endl;
    cout<<SCastInt(si)<<endl;
    cout<<SCastLong(sl)<<endl;
    cout<<SCastFloat(sf)<<endl;
    cout<<SCastDouble(sd)<<endl;
    
    cout<<"测试数值常量转string(无Ex)"<<endl;
    cout<<SCastInt("100")<<endl;
    cout<<SCastLong("200000")<<endl;
    cout<<SCastFloat("3.14")<<endl;
    cout<<SCastDouble("3.1415926")<<endl;
    
    cout<<"测试数值变量转string(有Ex)"<<endl;
    cout<<SCastIntEx(si).value<<endl;
    cout<<SCastLongEx(sl).value<<endl;
    cout<<SCastFloatEx(sf).value<<endl;
    cout<<SCastDoubleEx(sd).value<<endl;
    
    cout<<"测试数值常量转string(有Ex)"<<endl;
    cout<<SCastIntEx("100").value<<endl;
    cout<<SCastLongEx("200000").value<<endl;
    cout<<SCastFloatEx("3.14").value<<endl;
    cout<<SCastDoubleEx("3.1415926").value<<endl;
    
    int i=100;
    long l=200000;
    float f=3.14;
    double d=3.1415926;
    
    cout<<"测试string转数值变量(无Ex)"<<endl;
    cout<<ICastString(i)<<endl;
    cout<<LCastString(l)<<endl;
    cout<<FCastString(f)<<endl;
    cout<<DCastString(d)<<endl;    
    
    cout<<"测试string转数值常量(无Ex)"<<endl;
    cout<<ICastString(100)<<endl;
    cout<<LCastString(200000)<<endl;
    cout<<FCastString(3.14)<<endl;
    cout<<DCastString(3.1415926)<<endl;    
    
    cout<<"测试string转数值变量(有Ex)"<<endl;
    cout<<ICastStringEx(i).value<<endl;
    cout<<LCastStringEx(l).value<<endl;
    cout<<FCastStringEx(f).value<<endl;
    cout<<DCastStringEx(d).value<<endl;    
    
    cout<<"测试string转数值常量(有Ex)"<<endl;
    cout<<ICastStringEx(100).value<<endl;
    cout<<LCastStringEx(200000).value<<endl;
    cout<<FCastStringEx(3.14).value<<endl;
    cout<<DCastStringEx(3.1415926).value<<endl;    
    
    return 0;
}

//=============
//运行结果:
//=============
// 全面测试FCastInt
// 500
// 123
// 321
// 123
// 321
// 全面测试FCastIntEx
// 对str="123"
// str转换成功:123
// 对str="123A"
// str转换失败:0
// 测试数值变量转string(无Ex)
// 100
// 200000
// 3.14
// 3.14159
// 测试数值常量转string(无Ex)
// 100
// 200000
// 3.14
// 3.14159
// 测试数值变量转string(有Ex)
// 100
// 200000
// 3.14
// 3.14159
// 测试数值常量转string(有Ex)
// 100
// 200000
// 3.14
// 3.14159
// 测试string转数值变量(无Ex)
// 100
// 200000
// 3.1400001
// 3.1415926000000001
// 测试string转数值常量(无Ex)
// 100
// 200000
// 3.1400000000000001
// 3.1415926000000001
// 测试string转数值变量(有Ex)
// 100
// 200000
// 3.1400001
// 3.1415926000000001
// 测试string转数值常量(有Ex)
// 100
// 200000
// 3.1400000000000001
// 3.1415926000000001
//=============


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值