madebits.com
C O D E
Base32 Encoding - Source Code in C++
<<<

  Tools & Code
  webspider
  webnowse
  filewatcher
  adc
  vnotes
  ana
  magicsquare
  aes
  base32
  nsh
  mobcon
  hdb
  tgen
  urlsuite
  httppass
  albpad
  klarity
  poker
  shtypshqip
  roman
  calendar
  main






Software videos
Latest software videos!



Base32 Encoding

Base32 encoding uses only 32 symbols to encode any data byte array (of 255 possible byte values). The 32 symbols to encode the data can be any, but often they are printable characters, letters (A-Z) and numbers (0-9). There are 26 letters and 10 numerical characters that make 36 in total, so 4 of them can be removed. Some documents, such as RFC 3548, standardize the printable characters to be used for base 32 encoding, but the choice may vary.

  Details

The implementation code distinguishes between:

  1. encoding of data as base32, that is, expressing them as an byte array of values from 0 to 31, and
  2. mapping the base32 encoded data to a 32 characters alphabet.

The implementation is in C++, but is mostly ANSI C compatible. It uses, however, a __int64 type as an intermediate buffer, which may not be supported by all compilers. For gcc in Linux, replace the unsigned __int64 with unsigned long long int.

Encoding Data

In order to use encode a byte array, find out first how long the output base32 data buffer needs to be. Given that 32 values are less than 255 values, the encoded array will be somehow longer. Finding the encoded buffer length can be done as follows:


#define INPUT_LEN 123

unsigned char data255[INPUT_LEN] ;
// fill the data buffer with data
// ...

int encodeLength = Base32.GetEncode32Length(INPUT_LEN);
unsigned char data32[] = new char[encodeLength];

Then the data can be encoded in base32:


if(!Base32.Encode32(data255, INPUT_LEN, data32))
{
	//error
}

Mapping to a Base32 Alphabet

After the base32 encoding of the data, a mapping to an alphabet can be done as follows:


const char alphabet[] = "123456789ABCDEFGHJKMNPQRSTUVWXYZ";
Base32.Map32(data32, encodeLength, alphabet);

The data32 data values and mapped in place.

Reversing the Process

To reverse the process just repeat the symmetrical steps:


Base32.Unmap32(data32, encodeLength, alphabet);

As with mapping, the unmap is done in place. The decoding code follows:


int decodeLength = Base32.GetDecode32Length(data32);
char decode255[] = new char[decodeLength];

Base32.Decode32(data32, encodeLength, decode255);

Finally, when finished, free the buffers:


delete[] data32;
delete[] decode255;

  Download

Base32 Encoding

VersionTypeFileSizeComment
1.0.0source
~2KBC++ code



Latest news by Google

madebits.com
home | legal | search | contact | top

©2005-2008