MCPcopy Create free account
hub / github.com/WaykiChain/WaykiChain / EncodeBase32

Function EncodeBase32

src/commons/util/util.cpp:365–417  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

363}
364
365string EncodeBase32(const unsigned char* pch, size_t len) {
366 static const char* pbase32 = "abcdefghijklmnopqrstuvwxyz234567";
367
368 string strRet = "";
369 strRet.reserve((len + 4) / 5 * 8);
370
371 int mode = 0, left = 0;
372 const unsigned char* pchEnd = pch + len;
373
374 while (pch < pchEnd) {
375 int enc = *(pch++);
376 switch (mode) {
377 case 0: // we have no bits
378 strRet += pbase32[enc >> 3];
379 left = (enc & 7) << 2;
380 mode = 1;
381 break;
382
383 case 1: // we have three bits
384 strRet += pbase32[left | (enc >> 6)];
385 strRet += pbase32[(enc >> 1) & 31];
386 left = (enc & 1) << 4;
387 mode = 2;
388 break;
389
390 case 2: // we have one bit
391 strRet += pbase32[left | (enc >> 4)];
392 left = (enc & 15) << 1;
393 mode = 3;
394 break;
395
396 case 3: // we have four bits
397 strRet += pbase32[left | (enc >> 7)];
398 strRet += pbase32[(enc >> 2) & 31];
399 left = (enc & 3) << 3;
400 mode = 4;
401 break;
402
403 case 4: // we have two bits
404 strRet += pbase32[left | (enc >> 5)];
405 strRet += pbase32[enc & 31];
406 mode = 0;
407 }
408 }
409
410 static const int nPadding[5] = {0, 6, 4, 3, 1};
411 if (mode) {
412 strRet += pbase32[left];
413 for (int n = 0; n < nPadding[mode]; n++) strRet += '=';
414 }
415
416 return strRet;
417}
418
419string EncodeBase32(const string& str) {
420 return EncodeBase32((const unsigned char*)str.c_str(), str.size());

Callers 2

ToStringIPMethod · 0.85
BOOST_AUTO_TEST_CASEFunction · 0.85

Calls 2

reserveMethod · 0.80
sizeMethod · 0.45

Tested by 1

BOOST_AUTO_TEST_CASEFunction · 0.68