1. 程式人生 > 其它 >批量把16進位制轉成10進位制(c++)特別長的16進位制字串

批量把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