--- 已经通过初步测试----
------------------ 下面的是传统常见数据类型的转换(非c++11)---------------
std::string 与其他常用类型相互转换,
CString 与其他常见类型相互转换,
包括: int, char*, float, double, long。
自己写个类,方便调用, 包括:
MFC
A、int 转 CString
B、double 转 CString
C 、float 转 CString
D、long 转 CString
E、char * 转CString
F、CString 转 int 、double 、float、long、char * 。
C++
A、int 转 std::string
B、 double 转 std::string
C 、 float 转 std::string
D、long 转 std::string
E、char * 转 std::string
F、std::string 转 int, long , float, double ,char *
源码( .hpp 文件 )
1 // 照写
2 #pragma once
3 // MFC CString需要的头文件
4 #include <afxstr.h>
5 // 标准C++用到的头文件
6 #include <string>
7 #include <sstream>
8
9 // 命名空间
10 namespace util
11 {
12
13 // 进制枚举
14 enum ubase
15 {
16 // 转为10进制
17 base_to_10 = 10,
18 // 转为16进制
19 base_to_16 = 16,
20 };
21
22 // 各种数据类型转换
23 class data_trans
24 {
25 public:
26 // CString 转 其他
27 //--------------------------------
28 // CString 转 long(int) 参数【base】表示str的进制,
29 static int cs_to_l(const CString str, const int base = 10)
30 {
31 // 方法1
32 // return _wtoi(str);
33
34 // 方法2
35 USES_CONVERSION;
36 // 第一个参数:字符串, 第二个参数,默认0, 第三个参数:进制,10-10进制,16-16进制
37 return strtol(W2A(str), 0, base);
38 }
39
40 // CString 转 double
41 static double cs_to_d(const CString str)
42 {
43 return _wtof(str);
44 }
45
46 // Cstring 转 double、float
47 template <typename T>
48 static T cs_to_f(const CString str)
49 {
50 return _wtof(str);
51 }
52
53 // CString 转 char *
54 static char * cs_to_pchar(const CString str)
55 {
56 USES_CONVERSION;
57 return W2A(str);
58 }
59
60
61 // 其他 转 CString
62 //--------------------------------
63 // int、long 转 CString
64 template<typename T>
65 static CString i_to_cs(const T val, const ubase base = base_to_10)
66 {
67 CString str;
68
69 switch (base)
70 {
71 // 转为16进制的字符串
72 case base_to_16:
73 str.Format(L"%X", val);
74 break;
75
76 // 转为10进制的字符串
77 default:
78 str.Format(L"%d", val);
79 break;
80 }
81
82 return str;
83 }
84
85
86 // float, double转Cstring
87 template <typename T>
88 static CString f_to_cs(const T val)
89 {
90 CString str;
91 str.Format(L"%.f", val);
92
93 return str;
94 }
95
96 // char* 转Cstring
97 static CString pchar_to_cs(const char* parr)
98 {
99 // 为空? 那就返回空
100 if (NULL == parr || nullptr == parr)
101 return CString(L"");
102
103 CString str(parr);
104
105 return str;
106 }
107
108 // 下面是C++的一些转换
109 //------------------------------------------------------------------
110 // std::string 转其他
111
112 // std::string 转 int
113 static int s_to_i(const std::string str)
114 {
115 // str.c_str()返回的是一个char*的字符串
116 return atoi(str.c_str());
117 }
118
119 // std::string 转long、int 参数【base】是 指 str的进制
120 static long s_to_l(const std::string str, const int base = 10)
121 {
122 return strtol(str.c_str(), 0, base);
123 }
124
125 // std::string 转 float
126 static double s_to_f(const std::string str)
127 {
128 return atof(str.c_str());
129 }
130
131 // std::string 转 char*, 必须传递引用,否则,返回结果为NULL
132 static char * s_to_pchar(const std::string& str)
133 {
134 return (char *)str.c_str();
135 }
136
137
138 //--------------------------------------------------------
139 // 其他(int、double、float、long、shot、)转 std::string
140 //---------------------------------------------------------
141 template<typename T>
142 // value to string
143 static std::string val_to_s(const T val)
144 {
145 std::string s;
146 std::stringstream ss;
147 ss << val;
148 ss >> s;
149 return s;
150 }
151
152 }; // 类结束
153
154 } // 命名空间结束
类 调用范例:
CString转其他常见类型
CString转int
cs_int = static_cast<int>(util::data_trans::cs_to_l(L"100"));
CString转long
cs_l = util::data_trans::cs_to_l(L"10",16);
CString转float
cs_f = util::data_trans::cs_to_f<float>(L"102");
CString转char*
parr = util::data_trans::cs_to_pchar(L"103");
其他常见类型转CString
int转CString
CString cstr_int = util::data_trans::i_to_cs<int>(cs_int);
long转CString
CString cstr_long = util::data_trans::i_to_cs<long>(cs_l, util::ubase::base_to_16);
double转CString
CString cstr_double = util::data_trans::f_to_cs<double>(cs_d);
float转CString
CString cstr_float = util::data_trans::f_to_cs<float>(cs_f);
char *转CString
CString cstr_parr = util::data_trans::pchar_to_cs("ABC");
下面是: c++相关转换
std::string转int
s_int = static_cast<int>(util::data_trans::s_to_l(("100"),10));
std::string转long
s_long = util::data_trans::s_to_l(("100"),16);
std::string转double
s_d = util::data_trans::s_to_f("102.8");
std::string转char *
std::stringstr_tmp("103"); parr= util::data_trans::s_to_pchar(str_tmp);
-------其他常见类型转std::string---------
int转std::string
std::stringstr_int = util::data_trans::val_to_s<int>(200);
float转std::string
std::stringstr_float = util::data_trans::val_to_s<float>(200.3);
double转std::string
std::stringstr_double = util::data_trans::val_to_s<double>(202.3);
long转std::string
std::stringstr_long = util::data_trans::val_to_s<long>(1000);
char *转std::string
std::stringstr_char = util::data_trans::val_to_s<char*>("ABC");
-------完整示例代码------------
1 int cs_int = 0;
2 double cs_d = 0.0;
3 float cs_f = 0.0;
4 char *parr = NULL;
5 long cs_l = 0;
6
7 // Cstring 转各种
8 cs_int = static_cast<int>(util::data_trans::cs_to_l(L"100"));
9 cs_l = util::data_trans::cs_to_l(L"10", 16);
10 cs_d = util::data_trans::cs_to_d(L"101");
11 cs_f = util::data_trans::cs_to_f<float>(L"102");
12 parr = util::data_trans::cs_to_pchar(L"103");
13
14 // 各种转Cstring
15 CString cstr_int = util::data_trans::i_to_cs<int>(cs_int);
16 CString cstr_long = util::data_trans::i_to_cs<long>(cs_l, util::ubase::base_to_16);
17 CString cstr_double = util::data_trans::f_to_cs<double>(cs_d);
18 CString cstr_float = util::data_trans::f_to_cs<float>(cs_f);
19 CString cstr_parr = util::data_trans::pchar_to_cs("ABC");
c++
1 int s_int = 0;
2 double s_d = 0.0;
3 float s_f = 0.0;
4 char *parr = NULL;
5 long s_long = 0;
6 std::string str_tmp("103");
7
8 // string 转 各种
9 s_int = static_cast<int>(util::data_trans::s_to_l(("100"), 10));
10 s_long = util::data_trans::s_to_l(("100"), 16);
11 s_d = util::data_trans::s_to_f("102");
12 parr = util::data_trans::s_to_pchar(str_tmp);
13
14
15 // 各种转string
16 std::string str_int = util::data_trans::val_to_s<int>(200);
17 std::string str_float = util::data_trans::val_to_s<float>(200.3);
18 std::string str_double = util::data_trans::val_to_s<double>(202.3);
19 std::string str_long = util::data_trans::val_to_s<long>(1000);
20 std::string str_char = util::data_trans::val_to_s<char*>("ABC");
【3/9/2020 17:38】 增加 c++11新特性函数完成数据类型转换
-------------- 下面的是传统常见数据类型的转换(c++11)---------------
需要包含头文件
#include <string>
先 上源码, 做个类, 方便调用:
1 // c++11新增的函数实现类型转换
2 class data_trans11
3 {
4 public:
5 // 其他常见类型 转 std::string
6 // 包括:int、double、long、long long、 long double
7 // unsigned long, unsigned long long,
8 template<typename T>
9 static std::string to_str(const T val)
10 {
11 return to_string(val);
12 }
13
14 // std::string 转 int
15 // 参数【base】表示【str】的进制
16 static int s_to_i(const std::string str, const int base = 10)
17 {
18 return std::stoi(str, 0, base);
19 }
20
21 // std::string 转 float
22 static float s_to_f(const std::string str)
23 {
24 return std::stof(str);
25 }
26
27 // std::string 转 double
28 static double s_to_d(const std::string str)
29 {
30 return std::stod(str);
31 }
32
33 // std::string 转 long
34 // 参数【base】表示【str】的进制
35 static long s_to_l(const std::string str, const int base = 10)
36 {
37 return std::stol(str, 0, base);
38 }
39
40 // std::string 转 unsigned long
41 // 参数【base】表示【str】的进制
42 static unsigned long s_to_ul(const std::string str, const int base = 10)
43 {
44 return std::stoul(str, 0, base);
45 }
46
47 // std::string 转 long long
48 // 参数【base】表示【str】的进制
49 static long long s_to_ll(const std::string str, const int base = 10)
50 {
51 return std::stoll(str, 0, base);
52 }
53
54 // std::string 转 unsigned long long
55 // 参数【base】表示【str】的进制
56 static unsigned long long s_to_ull(const std::string str, const int base = 10)
57 {
58 return std::stoull(str, 0, base);
59 }
60
61 // std::string 转 long double
62 static long double s_to_ld(const std::string str)
63 {
64 return std::stold(str);
65 }
66 };
类 调用 范例如下:
-------常见数据类型转std::string------------------
包括:int、double、long、longlong、longdouble、unsignedlong,unsignedlong long转std::string
int转std::string
std::stringstr_int = util::data_trans11::to_str<int>(2);
double转std::string
std::stringstr_double = util::data_trans11::to_str<double>(3.3);
float转std::string
std::stringstr_float = util::data_trans11::to_str<float>(4.4);
long转std::string
std::stringstr_long = util::data_trans11::to_str<long>(5);
long long转std::string
std::stringstr_ll = util::data_trans11::to_str<longlong>(6);
unsigned long long转std::string
std::stringstr_ull = util::data_trans11::to_strlong\\long\>(7);
long double转std::string
std::stringstr_ld = util::data_trans11::to_str<longdouble>(8.8);
----------下面是字符串转其他常见类型---------------
std::string转int
v_int = util::data_trans11::s_to_i("11",16);
std::string转float
v_float = util::data_trans11::s_to_f("12.2");
std::string转double
v_double = util::data_trans11::s_to_d("13.3");
std::string转long
v_long = util::data_trans11::s_to_l("14",16);
std::string转unsigned long long
v_ull = util::data_trans11::s_to_ull("16",16);
std::string转long long
v_ll = util::data_trans11::s_to_ll("17",16);
std::string转long double
v_ld = util::data_trans11::s_to_ld("18.8");
欢迎指正。
原文链接: https://www.cnblogs.com/pandamohist/p/13606206.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/202267
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!