C++ 10进制, 16进制, ASCII码, 单字节与多字节的相互转换

这些简单的转换是用的比较频繁的, 因此将这些功能全部封装在一个类中

头文件

 1 #pragma once
 2 #include <stdlib.h>
 3 #include <string>
 4 #include "tchar.h"
 5 #include "windows.h"
 6 using namespace std;
 7 enum TRANSFORM
 8 {
 9     SINGLECHAR, //单字节转换    eg:0x2121 => 33 33
10     ALLCHAR,    //所有字节转换  eg:0x2121 => 8481
11 };
12 class Transform
13 {
14 public:
15     Transform(void);
16     ~Transform(void);
17 
18 public:
19     string HexStrToAsciiStr(string strValue);
20     string AsciiStrToHexStr(string strValue);
21     string HexToDescInt(string strVaule, TRANSFORM);
22     string DescIntToHex(string strValue);
23     string TCharToString(TCHAR* TValue);
24     void StringToTChar(string strValue, TCHAR* DestTchar);
25 private:
26     string strRepalce(string& strValue, string strSrc, string strDest);
27 };
View Code

cpp文件

  1 #include "Transform.h"
  2 #pragma warning(disable:4996)
  3 
  4 Transform::Transform(void)
  5 {
  6 
  7 }
  8 
  9 Transform::~Transform(void)
 10 {
 11 
 12 }
 13 
 14 string Transform::HexStrToAsciiStr(string strValue)
 15 {
 16     string strTemp;
 17     if(strValue.find("0x") != string::npos || strValue.find("0X") != string::npos)
 18     {
 19         if(strValue.find("0x") != string::npos)
 20             strTemp = strRepalce(strValue, "0x", "");
 21         else
 22             strTemp = strRepalce(strValue, "0X", "");
 23     }
 24     if(strTemp.length() %2 != 0)
 25         strTemp.insert(0, 1, '0');
 26     string strRet;
 27     int temp = 0;
 28     for(int i = 0; i < strTemp.length(); i++)
 29     {
 30         int flag = (i % 2 == 0) ? 16 : 1;
 31         if(strTemp.c_str()[i] > '0' && strTemp.c_str()[i] < '9')
 32         {
 33             temp += (strTemp.c_str()[i] - '0')*flag;
 34         }
 35         else if(strTemp.c_str()[i] == 'a' || strTemp.c_str()[i] == 'A')
 36         {
 37             temp += (10*flag);
 38         }
 39         else if(strTemp.c_str()[i] == 'b' || strTemp.c_str()[i] == 'B')
 40         {
 41             temp += (11*flag);
 42         }
 43         else if(strTemp.c_str()[i] == 'c' || strTemp.c_str()[i] == 'C')
 44         {
 45             temp += (12*flag);
 46         }
 47         else if(strTemp.c_str()[i] == 'd' || strTemp.c_str()[i] == 'D')
 48         {
 49             temp += (13*flag);
 50         }
 51         else if(strTemp.c_str()[i] == 'e' || strTemp.c_str()[i] == 'E')
 52         {
 53             temp += (14*flag);
 54         }
 55         else if(strTemp.c_str()[i] == 'f' || strTemp.c_str()[i] == 'F')
 56         {
 57             temp += (15*flag);
 58         }
 59         if(i % 2 != 0)
 60         {
 61             strRet+=temp;
 62             temp =0;
 63         }
 64     }
 65     return strRet;
 66 }
 67 
 68 string Transform::AsciiStrToHexStr(string strValue)
 69 {
 70     if(strValue.length() == 0)
 71         return "";
 72     char Src[1024] = { 0 };
 73     strcpy_s(Src, strValue.c_str());
 74     int len = strValue.length();
 75     char* Dest = (char*)calloc(2*len +1, sizeof(char));
 76     for( int i = 0, j = 0; i < strlen(strValue.c_str()); i++ )
 77     {
 78          char HiHalf = Src[i] >> 4;
 79          char LoHalf = Src[i] & 0x0F;
 80          Dest[j++] = ( HiHalf <= 9 ) ? ( HiHalf + '0' ) : ( HiHalf - 10 + 'A' );
 81          Dest[j++] = ( LoHalf <= 9 ) ? ( LoHalf + '0' ) : ( LoHalf - 10 + 'A' );
 82     }
 83     string strRet = Dest;
 84     delete[] Dest;
 85     Dest = NULL;
 86     return strRet;
 87 }
 88 
 89 string Transform::HexToDescInt(string strVaule, TRANSFORM tfType)
 90 {
 91     if(strVaule.find("0x") != string::npos || strVaule.find("0X") != string::npos)
 92     {
 93         if(strVaule.find("0x") != string::npos)
 94             strVaule = strRepalce(strVaule, "0x", "");
 95         else
 96             strVaule = strRepalce(strVaule, "0X", "");
 97     }
 98 
 99     if(strVaule.length() % 2 != 0)
100         strVaule.insert(0, 1, '0');
101     if(strVaule.length() == 0)
102         return "";
103     string strRet;
104     unsigned long iValue = 0;
105     switch(tfType)
106     {
107     case ALLCHAR:
108         {
109             for(int i = 0; i < strVaule.length(); i++)
110             {
111                 iValue += (strVaule.c_str()[i] - '0') << 4*(strVaule.length() - i -1);
112             }
113             strRet = to_string(iValue);
114         }break;
115     case SINGLECHAR:
116         {
117             for(int i = 0; i < strVaule.length(); i+=2)
118             {
119                 iValue += (strVaule.c_str()[i] - '0') << 4;
120                 if(strVaule.length() >= i+1)
121                 {
122                     iValue += (strVaule.c_str()[i+1] - '0');
123                 }
124                 strRet += (to_string(iValue) + " ");
125                 iValue = 0;
126             }
127         }break;
128     default:
129         break;
130     }
131     return strRet;
132 }
133 
134 string Transform::DescIntToHex(string strValue)
135 {
136     if(strValue.length() == 0)
137         return "";
138     if(strValue.length() % 2 != 0)
139         strValue.insert(0, 1, '0');
140     char temp[256] = { 0 };
141     int iValue = atoi(strValue.c_str());
142     itoa(iValue, temp, 16);
143     string strRet = "0x";
144     strRet+= temp;    
145     return strRet;
146 }
147 
148 string Transform::strRepalce(string& strValue, string strSrc, string strDest)
149 {
150     string::size_type pos = 0;
151     string::size_type srclen = strSrc.size();
152     string::size_type dstlen = strDest.size();
153 
154     while( (pos=strValue.find(strSrc, pos)) != std::string::npos )
155     {
156         strValue.replace( pos, srclen, strDest );
157         pos += dstlen;
158     }
159     return strValue;
160 }
161 
162 string Transform::TCharToString(TCHAR* TValue)
163 {
164     string strRet;
165     int iLength ;  
166     //获取字节长度   
167     iLength = WideCharToMultiByte(CP_ACP, 0, TValue, -1, NULL, 0, NULL, NULL);  
168     //将tchar值赋给_char    
169     char* szChar = (char*)calloc(iLength, sizeof(char));
170     WideCharToMultiByte(CP_ACP, 0, TValue, -1, szChar, iLength, NULL, NULL);  
171     strRet = szChar;
172     delete[] szChar;
173     szChar = NULL;
174     return strRet;
175 }
176 void Transform::StringToTChar(string strValue, TCHAR* DestTchar)
177 {
178     int iLength ;  
179     iLength = MultiByteToWideChar (CP_ACP, 0, strValue.c_str(), strlen (strValue.c_str()) + 1, NULL, 0);
180     MultiByteToWideChar (CP_ACP, 0, strValue.c_str(), strlen (strValue.c_str()) + 1, DestTchar, iLength) ;  
181 }
View Code

测试文件

 1 #include<stdio.h>
 2 #include "Transform.h"
 3 
 4 int main()
 5 {
 6     Transform m_obj;
 7     string value= "3135353734414EmM";
 8     string Hex = m_obj.AsciiStrToHexStr(value);
 9     printf("Hex = %s \n", Hex.c_str());
10 
11     string asic = m_obj.HexStrToAsciiStr("0x303132343335363738394d5e6f20212223");
12     printf("asci= %s\n", asic.c_str());
13 
14     string DecInt = m_obj.HexToDescInt("0x11122", ALLCHAR);
15     printf("DesInt = %s \n", DecInt.c_str());
16     DecInt = m_obj.HexToDescInt("0x11122", SINGLECHAR);
17     printf("DesInt = %s \n", DecInt.c_str());
18 
19     string hex = m_obj.DescIntToHex("1122");
20     printf("hex = %s\n", hex.c_str());
21 
22     string szChar = m_obj.TCharToString(_T("Hello World"));
23     printf("szChar = %s\n", szChar.c_str());
24 
25     TCHAR tempChar[256] ={ 0 };
26     m_obj.StringToTChar("Hello C++", tempChar);
27     
28     printf("tChar = %s\n", (char*)tempChar);
29     getchar();
30     return 0;
31 }
View Code

 

16进制字符串转10进制
调用系统函数pow(int x, int y)
x: 基数 y:指数

int HexToInt(string s)
{
        //STL 函数将字符串全部转大写
	transform(s.begin(), s.end(), s.begin(), ::toupper);
	int count = s.length();
	int sum = 0;
	for (auto it : s)
	{
                //A 的assic 为65.  it-55:表示A~F转10~15
		int flag = (it >= 65) ? (it - 55) : (it - 48);
		if (flag < 0 || flag > 15) continue;
		sum += flag *pow(16, --count);
	}
	printf("sum = %d\n", sum);
	return sum;
}        

  

PS: 代码写的有点low, 仅供参考 ~_~

posted @ 2018-07-25 16:43  Software_hul  阅读(1798)  评论(0编辑  收藏  举报