Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 527561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:53:45+00:00 2026-05-13T08:53:45+00:00

I am have written the following code below to encode a bitarray into custom

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T08:53:46+00:00Added an answer on May 13, 2026 at 8:53 am

    A few suggestions:

    • You don’t have any idea of “padding” – so you can’t
    • Pass the key characters in as a parameter to the method. You can have an overload for a “default” one if you want
    • Either give the StringBuilder a capacity to start with, or create a char array of the right length and create the string directly from that
    • Catching Exception is almost always a bad idea, and it certainly is here. Any exception would be due to a bug in this code, so just let it bubble up the stack
    • Comparisons with “true” always look smelly to me, and I personally put if/for/while/etc statement bodies in blocks even for single statements, so I would have

      if (ba.Get(i))
      {
          bitValue[0] = 1;
      }
      
    • There’s no real point in having the bit array to start with. Why not just add to a value which starts at 0?

      if (ba.Get(i))
      {
          temp += 16;
      }
      // etc
      
    • Repeated code like that suggests a loop:

      int temp = 0;
      for (int j = 0; j < 5; j++)
      {
          if (ba.Get(i + j))
          {
              // This could just be "1 << j" if you don't mind the
              // results being different to your current code
              temp += 1 << (4 - j);
          }
      }
      
    • Library methods shouldn’t write to the console
    • Don’t call ToString when you’ve got the right character – just call Append(char) (or set the value in the result char array).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have written the following code class Program { static void Main(string[] args) {
i have written the following code: As you can see there is a for
I have written the following code and getting the below error, I believe its
I hope someone can help me with the following... I have this code below
I have written following code to attach gesture recogniser to multiple imageviews. [imageview1 setUserInteractionEnabled:YES];
I am trying my hands on WPF MVVM. I have written following code in
I have written the following code, CrystalDecisions.CrystalReports.Engine.ReportDocument report = new CrystalDecisions.CrystalReports.Engine.ReportDocument(); report.Load(@C:\Users\XXX\Desktop\Backup1\Project\ReportsFolder\ReportSalesInvoice.rpt); Report works
I have written a following code to get just the file name without extension
I have written the following code choice /m Do you want to add another
I have written the following code. But it is removing only &nbsp; not <br>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.