Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static const ubyte base32_PAD = '=';
- static const char[] _encodeTable = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
- /*******************************************************************************
- encodes data and returns as an ASCII base32 string.
- *******************************************************************************/
- char[] encode(ubyte[] data, char[] buff, bool pad=true)
- in
- {
- assert(data);
- assert(buff.length >= allocateEncodeSize(data));
- }
- body
- {
- uint i = 0;
- ushort remainder; // Carries overflow bits to next char
- byte remainlen; // Tracks bits in remainder
- foreach (ubyte j; data)
- {
- remainder = (remainder<<8) | j;
- remainlen += 8;
- do {
- remainlen -= 5;
- buff[i++] = _encodeTable[(remainder>>remainlen)&0b11111];
- } while (remainlen > 5)
- }
- if (remainlen)
- buff[i++] = _encodeTable[(remainder<<(5-remainlen))&0b11111];
- if (pad) {
- for (ubyte padCount=(-i%8);padCount > 0; padCount--)
- buff[i++] = base32_PAD;
- }
- return buff[0..i];
- }
- /*******************
- * Called like this
- ******************/
- scope char[64] idHexBuf;
- auto idHex = base32.encode(asset.localId, idHexBuf);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement