批量把16进制转成10进制(c++)特别长的16进制字符串

缘由:批量把特别长的16进制字符串,转成10进制,而excel转完会缺失很多位。

下面是C++代码,运行完,断点查看转换结果,或者打印出来也行

 1 /*
 2 * 将十六进制数字组成的字符串(不包含前缀0x或0X)转换为与之等价的整型值
 3 */
 4 #include <stdio.h>
 5 #include <iostream>
 6 #include <string>
 7 #include <vector>
 8 #include <math.h>
 9 using namespace std;
10 /* 将十六进制中的字符装换为对应的整数 */
11 long long hexchtoi(char hexch)
12 {
13     char phexch[] = "ABCDEF";
14     char qhexch[] = "abcdef";
15     for (long long i = 0; i < 6; i++)
16         if ((hexch == phexch[i]) || (hexch == qhexch[i]))
17             return 10 + i;
18 
19     return hexch-48; /* 非十六进制字符 */
20 }
21 long long htoi(const char s[])
22 {
23     long long n = 0; /*有n位*/
24     long long i = 0;
25     long long answer = 0;
26     /* 有效性检查 */
27     while ((s[i] != '\0')) {
28         if ((s[i] < '0') && (s[i] > '9'))
29             if (hexchtoi(s[i]) >= 0)
30                 return 0;
31         n++;
32         i++;
33     }
34     for (long long j = 0; j < n; j++)
35         answer += ((long long)pow(16, j) * hexchtoi(s[i - j - 1]));
36 
37     return answer;
38 }
39 int main()
40 {
41     vector<const char*> vecInput;
42     vecInput.push_back("11F000000000233C");
43     vecInput.push_back("11F000000000100B");
44     vecInput.push_back("11F00000000007A8");
45     vecInput.push_back("11F000000000145B");
46     vecInput.push_back("11F0000000000F18");
47     vector<long long> vecRes;
48     for (size_t i = 0; i < vecInput.size(); i++)
49     {
50         vecRes.push_back(htoi(vecInput[i]));
51     }
52     printf("-------stop----------");
53 }
View Code

 

posted @ 2022-03-13 16:23  菜鸡徐思  阅读(296)  评论(0编辑  收藏  举报