Base58編碼由58個(gè)數(shù)字和大小寫(xiě)字母組成,BitCoin源碼中定義及注釋如下:
成都創(chuàng)新互聯(lián)公司于2013年開(kāi)始,是專(zhuān)業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元漯河做網(wǎng)站,已為上家服務(wù),為漯河各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話(huà):13518219792
/** All alphanumeric characters except for "0", "I", "O", and "l"*/
static const char* pszBase58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
如unsigned char ucData[4] = { 0x39, 0x3a, 0x3b, 0x3c };的base58編碼過(guò)程如下:
1、先計(jì)算ucData開(kāi)頭為0x00的個(gè)數(shù) zeros ,這里zeros = 0;
while (pbegin != pend && *pbegin == 0) {
pbegin++;
zeroes++;
}
2、跳過(guò)開(kāi)頭的zeros個(gè)0x00,計(jì)算所需要的緩存
int size = (pend - pbegin) * 138 / 100 + 1; // log(256) / log(58), rounded up.
3、256進(jìn)制轉(zhuǎn)58進(jìn)制的計(jì)算
std::vector
// Process the bytes.
while (pbegin != pend) {
int carry = *pbegin;
int i = 0;
// Apply "b58 = b58 * 256 + ch".
for (std::vector
(carry != 0 || i < length) && (it != b58.rend()); it++, i++) {
carry += 256 * (*it);
*it = carry % 58;
carry /= 58;
}
assert(carry == 0);
length = i;
pbegin++;
}
4、輸出編碼結(jié)果
先在字符串前補(bǔ)上zeros 個(gè)1 ,后面的依次綴加DstByte對(duì)應(yīng)的 pszBase58 字符