I added comments to the function fromBase32
can the experienced users here read through my comments and respond if my understanding of what the code does is correct?
int Base32::fromBase32(byte* in, long length, byte*& out) {
int result = 0; // Length of the array of decoded values.
int buffer = 0;
int bitsLeft = 0;
byte* temp = NULL;
temp = (byte*)malloc(length); // Allocating temporary array.
for (int i = 0; i < length; i++) {// for-loop to iterate through the bytes the parameter "in" is pointing to
byte ch = in[i]; // assign a single character of the "input" byte-sequence to ch
// look up one base32 symbols: from 'A' to 'Z' or from 'a' to 'z' or from '2' to '7'
if ((ch >= 0x41 && ch <= 0x5A) || (ch >= 0x61 && ch <= 0x7A)) { ch = ((ch & 0x1F) - 1); }
else if (ch >= 0x32 && ch <= 0x37) { ch -= (0x32 - 26); }
else { free(temp); return 0; }
buffer <<= 5;
buffer |= ch;
bitsLeft += 5;
// if more than 8 bits are processed in the conversion
if (bitsLeft >= 8)
{
// store this byte into array "temp"
temp[result] = (unsigned char)((unsigned int)(buffer >> (bitsLeft - 8)) & 0xFF);
result++;
bitsLeft -= 8;
}
}
// allocate temporary memory in the length of "result" bytes
// and use "out" as the name for the pointer-variable pointing to this adress in RAM
out = (byte*)malloc(result);
memcpy(out, temp, result); // copy byte-sequence stored at "temp" to RAM-location where "out" points to
free(temp); // "mark RAM-area formely reserved for "temp" as free to use
return result;
}
which means after leaving the function fromBase32 the variable handed over by the call to the function
as the third parameter is pointing to that area in RAM where the converted byte-sequence is stored?
Now If I would like to use an array of byte as the third parameter
byte myTargetForConvertedASCII_HexKey[16];
and that this variable called "myTargetForConvertedASCII_HexKey" should contain the converted byte-sequence
How do I have to modify the function?
What I find difficult is that I have to "hand-over" an array of bytes as a parameter or do I hand-over a pointer to the beginning of the byte-array?
If yes how does this look like?
byte mySourceArray[32];
byte myTargetArray[32];
void demoFunc (//not sure how the definition must look like) {
void demoFunc("myTargetArray", "mySourceArray") {
best regards Stefan