convert from decimal(base-10) to alphanumeric(base-36)

dear All,

I am creating a program that generate id for any incoming material lot using hexadecimal ID, Since the number of incoming material lot increasing rapidly, we plan to move to a new ID number with alphanumeric(0-9,A-Z).

The convert class doesn't support the base 36 type. Does anyone have any idea for my problem Any help is very appreciated.

Thank you

regards

Freddy Halim



Answer this question

convert from decimal(base-10) to alphanumeric(base-36)

  • sumit kr

    Glad it helped.
    If you need any help converting from base 36 -> base 10 just ask. But it shouldn't be difficult. :)


  • hazz

    Hi,
    check if works for you:

    public String ConvertToBase(int num, int nbase)
    {
    String chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    // check if we can convert to another base
    if(nbase < 2 || nbase > chars.Length)
    return "";

    int r;
    String newNumber = "";

    // in r we have the offset of the char that was converted to the new base
    while(num >= nbase)
    {
    r = num % nbase;
    newNumber = chars[r] + newNumber;
    num = num / nbase;
    }
    // the last number to convert
    newNumber = chars[num] + newNumber;

    return newNumber;
    }



  • simon_simon

    Actually, you may want to use the standard Base-40 encoding (which is supported by .Net)

    for(long i = 100000; i < 200000; ++i)
    {
    string enc = Convert.ToBase64String(BitConverter.GetBytes(i));
    long dec = BitConverter.ToInt64(Convert.FromBase64String(enc), 0);
    Console.WriteLine("
    From {0}, to {1}, to {2}", i, enc, dec);
    }



  • Gravy

    n0n4m3,

    I was hoping that you could help me by posting the code to convert from base-36 to base-10

    Thanks in advance,

    MMcNamara


  • MacedonHacker

    Dear n0n4me,

    Yeah its works. Looking at your code, i think i can start create the opposite conversion from base36 to base10 too.

    Thank you for your help. Your help is very appreciated and invaluable for me.

    Warmest Regards,

    Freddy Halim


  • convert from decimal(base-10) to alphanumeric(base-36)