如何實現快速將 64 位二進位(存在字元串里)轉換為十進位?

不使用long或long long


更新:之前缺少了進位處理。

我理解這個題目是說,不轉換成64位整數,直接把二進位的字元串轉換成十進位。

設二進位為b_{63}b_{62}ldots b_1b_0,那麼第 j 個十進位為

d_j = left lfloor frac{sum 2^i b_i}{10^j} 
ight 
floor mod 10

為了直接計算二進位各位的和,可以轉換成以下的形式(c_j為進位)

d_j + c_j= sum b_i left (left lfloor frac{ 2^i }{10^j} 
ight 
floor mod 10 
ight)

那麼,我們只需要預計算中間那個基於(i, j) 的表(每個元素的值 &<10 ),就可以求出每個十進位的值:

#include &
#include &
#include &
#include &

static uint8_t weight[64 * 19] = {
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
6,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
2,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
4,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
8,2,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
6,5,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
2,1,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
4,2,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
8,4,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
6,9,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
2,9,1,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
4,8,3,6,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
8,6,7,2,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
6,3,5,5,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
2,7,0,1,3,1,0,0,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0,0,0,0,0,0,
6,1,4,4,4,0,6,8,1,2,9,5,7,1,0,0,0,0,0,
2,3,8,8,8,0,2,7,3,4,8,1,5,3,0,0,0,0,0,
4,6,6,7,7,1,4,4,7,8,6,3,0,7,0,0,0,0,0,
8,2,3,5,5,3,8,8,4,7,3,7,0,4,1,0,0,0,0,
6,5,6,0,1,7,6,7,9,4,7,4,1,8,2,0,0,0,0,
2,1,3,1,2,4,3,5,9,9,4,9,2,6,5,0,0,0,0,
4,2,6,2,4,8,6,0,9,9,9,8,5,2,1,1,0,0,0,
8,4,2,5,8,6,3,1,8,9,9,7,1,5,2,2,0,0,0,
6,9,4,0,7,3,7,2,6,9,9,5,3,0,5,4,0,0,0,
2,9,9,0,4,7,4,5,2,9,9,1,7,0,0,9,0,0,0,
4,8,9,1,8,4,9,0,5,8,9,3,4,1,0,8,1,0,0,
8,6,9,3,6,9,8,1,0,7,9,7,8,2,0,6,3,0,0,
6,3,9,7,2,9,7,3,0,4,9,5,7,5,0,2,7,0,0,
2,7,8,5,5,8,5,7,0,8,8,1,5,1,1,4,4,1,0,
4,4,7,1,1,7,1,5,1,6,7,3,0,3,2,8,8,2,0,
8,8,4,3,2,4,3,0,3,2,5,7,0,6,4,6,7,5,0,
6,7,9,6,4,8,6,0,6,4,0,5,1,2,9,2,5,1,1,
2,5,9,3,9,6,3,1,2,9,0,0,3,4,8,5,0,3,2,
4,0,9,7,8,3,7,2,4,8,1,0,6,8,6,1,1,6,4,
8,0,8,5,7,7,4,5,8,6,3,0,2,7,3,3,2,2,9,
};

// s must be [01]{1,64}
std::string bin2dec(const std::string s) {
assert(s.length() &>= 0 s.length() &<= 64); uint8_t sum[20] = { 0 }; for (int i = 0; i &< s.length(); i++) if (s[i] == "1") for (int j = 0; j &< 19; j++) { sum[j] += weight[(s.length() - 1 - i) * 19 + j]; for (int c = j; sum[c] &>= 10; c++) {
sum[c] -= 10;
sum[c + 1]++;
}
}
else
assert(s[i] == "0");

// First non-zero digit
int k = 19;
while (k &> 0 sum[k] == 0)
k--;

// reverse output
std::string o;
o.reserve(k + 1);
for (int j = 0; j &<= k; j++) o.push_back(sum[k - j] + "0"); return o; } int main(int argc, char* argv[]) { // Generate weight table // uint64_t divisor = 1; // for (int j = 0; j &< 19; j++) { // for (int i = 0; i &< 64; i++) { // weight[i * 19 + j] = static_cast&(((uint64_t(1) &<&< i) / divisor) % 10); // } // divisor *= 10; // } // for (int i = 0; i &< 64; i++) { // for (int j = 0; j &< 19; j++) // std::cout &<&< (int)weight[i * 19 + j] &<&< ","; // std::cout &<&< std::endl; // } std::cout &<&< bin2dec(argv[1]) &<&< std::endl; }

bin2dec() 中避免了使用除法和模除,它使用減法來實現算式中第二個模除。

可以看到,表裡有很多 0 ,其實可以再做一個表去表示真正需要的長度,大家可以嘗試一下。

另外,這個做法實際上可能比正常做法(先轉成整數再轉成10進數字元串)為慢,但可以免去除法和模除,或適合超過64位而又有限長度的輸入。


#include &

// std::string binary
// unsigned long long value
value = std::stoull(binary, nullptr, 2);

或者

#include &

// std::string binary
// unsigned long long value
value = std::stroull(binary.c_str(), nullptr, 2);

兩者沒有大區別,卻需要有不同的出錯處理。

# 抱歉,下面答成十六進位轉換了。請忽略。

C++ 風格

#include &
#include &

// std::string hex
// uint64_t value
std::istringstream(hex) &>&> std::hex &>&> value;

C 風格

#include &
#include &

// std::string hex
// uint64_t value
std::sscanf(hex.c_str(), "%" SCNx64, value);


#include &
#include &
#include &

unsigned long convert_to_ulong(const std::string str)
{
return std::bitset&(str).to_ulong();
}


推薦閱讀:

C++ 如何跨平台判斷操作系統是32位還是64位?
在鏈表中應該用哪種智能指針比較合理?
什麼時候適合使用 C++ 而不是 C?
除了emoji,有沒有用utf16兩個位元組表示不了而且現代文章/姓名中會使用的cjkv字元么?
c++如何做設計?或者推薦一些比較簡單的開源項目,適合新手練手的。

TAG:演算法 | 編程 | 計算機科學 | C | 二進位 |