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

  • SEARCH
  • Home
  • 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 6886725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:47:44+00:00 2026-05-27T05:47:44+00:00

I am using the following class method to create a Base36 string from a

  • 0

I am using the following class method to create a Base36 string from a number:

private const string CharList = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

public static String Encode(long input) {
    if (input < 0) throw new ArgumentOutOfRangeException("input", input, "input cannot be negative");
        char[] clistarr = CharList.ToCharArray();
        var result = new Stack<char>();
        while (input != 0)
        {
            result.Push(clistarr[input % 36]);
            input /= 36;
        }
        return new string(result.ToArray());
    }

One of the requirements is that the string should always be padded with zero’s and should be a maximum of four digits. Can anyone suggest a way that I can code the leading zero’s and also limit the function so it will never return more than “ZZZZ” ? Is there some function in C# that can do this. Sorry about the indentation on this code. I am not sure why it’s not indenting properly.

  • 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-27T05:47:45+00:00Added an answer on May 27, 2026 at 5:47 am

    If there are always going to be exactly four digits, it’s really easy:

    const long MaxBase36Value = (36L * 36L * 36L * 36L) - 1L;
    
    public static string EncodeBase36(long input)
    {
        if (input < 0L || input > MaxBase36Value)
        {
            throw new ArgumentOutOfRangeException();
        }
        char[] chars = new char[4];
        chars[3] = CharList[(int) (input % 36)];
        chars[2] = CharList[(int) ((input / 36) % 36)];
        chars[1] = CharList[(int) ((input / (36 * 36)) % 36)];
        chars[0] = CharList[(int) ((input / (36 * 36 * 36)) % 36)];
        return new string(chars);
    }
    

    Or using a loop:

    const long MaxBase36Value = (36L * 36L * 36L * 36L) - 1L;
    
    public static string EncodeBase36(long input)
    {
        if (input < 0L || input > MaxBase36Value)
        {
            throw new ArgumentOutOfRangeException();
        }
        char[] chars = new char[4];
        for (int i = 3; i >= 0; i--)
        {
            chars[i] = CharList[(int) (input % 36)];
            input = input / 36;            
        }
        return new string(chars);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a UserBll class which has following method public string GetaspnetUserIdByUserName(string name )
I am trying to register the following class using the fluent interface: public class
I'm using the following mapping: public class LoadMap : IAutoMappingOverride<Load> { public void Override(AutoMapping<Load>
I'm using the following code within the JCProperty class to retrieve data from a
Using the following Webservice definition using aClientArgs as a complex type: [System.Web.Script.Services.ScriptService] public class
I am using the SQLite database and have the following persistent class (simplified): public
I have a windows service which is using a method from a class library
Using following method to create Shopping Cart Session http://net.tutsplus.com/tutorials/other/build-a-shopping-cart-in-aspnet/ Now running locally on my
Consider the following class: public class MyIntSet { private List<int> _list = new List<int>();
I am using following class to show a grid like appearance with pagination on

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.