I am have written the following code below to encode a bitarray into custom base32 encoding string. My idea is user can mix the order of base32 array as per requirement and can add similar looking characters like I and 1 etc.
My intention of asking the question is: Is the code written in an appropriate manner or it lacks some basics. As far as i know it is generating the output as per the requirment, however i want to just validate the code here. If there are flaws do let me know.
A user will have a string which needs to be base32 encoded. So in his function he would call it this way.
BitArray ba = new BitArray(Encoding.UTF8.GetBytes(CustomString));
GenerateBase32FromString(ba);
Now the GenerateBase32FromString is as below
static string GenerateStringFromKey(BitArray ba)
{
try
{
// user will modify the order as per requirement.
char[] cProdKeyPoss = "ABGCD1EF2HI3KL4MN5PQ6RS7TV8WX9YZ".ToCharArray();
StringBuilder finalstring = new StringBuilder();
// add zero value bits to round off to multiple of 5
//ba.Length = ba.Length + (5-(ba.Length % 5));
// remove bits to round off to multiple of 5
ba.Length = ba.Length - (ba.Length % 5);
Console.WriteLine("ba.length = " + ba.Length.ToString());
for (int i = 0; i < ba.Length; i = i + 5)
{
int[] bitvalue = { 0, 0, 0, 0, 0 };
if (ba.Get(i) == true)
bitvalue[0] = 1;
if (ba.Get(i + 1) == true)
bitvalue[1] = 1;
if (ba.Get(i + 2) == true)
bitvalue[2] = 1;
if (ba.Get(i + 3) == true)
bitvalue[3] = 1;
if (ba.Get(i + 4) == true)
bitvalue[4] = 1;
int temp = (16 * bitvalue[0]) + (8 * bitvalue[1]) + (4 * bitvalue[2]) + (2 * bitvalue[3]) + (bitvalue[4]);
finalstring.Append(cProdKeyPoss[temp].ToString());
}
Console.WriteLine(finalstring.ToString());
return finalstring.ToString();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return null;
}
}
I have kept both the options where i will chop the bits to round of to multiple of 5 or will add additional zero value bits to make it multiple of 5.