BASE58 encoding consists of 58 numbers and uppercase and lowercase letters, the bitcoin source code is defined and annotated as follows:
/** all alphanumeric characters except for "0", "I", "O", and "L " */
static const char* PSZBASE58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZABCDEFGHIJKMNOPQRSTUVWXYZ";
such as unsigned char ucdata[4] = {0x39, 0x3a, 0x3b, 0x3c}, the BASE58 encoding process is as follows:
1, first calculate ucdata start for 0x00 number zeros, here zeros = 0;
while (pbegin! = Pend && *pbegin = = 0) {
pbegin++;
zeroes++;
}
2, skip the beginning of zeros 0x00, calculate the required cache
int size = (pend-pbegin) * 138/100 + 1; Log (+)/log (rounded), up.
3, 256 The calculation of the binary turn 58 binary
std::vector<unsigned char> b58 (size);
Process the bytes.
while (pbegin! = pend) {
int carry = *pbegin;
int i = 0;
Apply "b58 = b58 * + ch".
For (std::vector<unsigned char>::reverse_iterator it = B58.rbegin ();
(Carry! = 0 | | I < length) && (It! = B58.rend ()); it++, i++) {
Carry + = * (*it);
*it = carry% 58;
Carry/= 58;
}
ASSERT (Carry = = 0);
length = i;
pbegin++;
}
4. Output encoding Result
Precede the string with zeros 1, followed by the prefix plus dstbyte corresponding pszBase58 character
This article from the "Clear" blog, reproduced please contact the author!
Bitcoin source code Research (1)-BASE58 code