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

    Question

  • 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

    Monday, May 22, 2006 3:46 AM

Answers

  • 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;
    }

    Monday, May 22, 2006 6:41 AM

All replies

  • 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;
    }

    Monday, May 22, 2006 6:41 AM
  • 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

    Monday, May 22, 2006 7:42 AM
  • Glad it helped.
    If you need any help converting from base 36 -> base 10 just ask. But it shouldn't be difficult. :)
    Monday, May 22, 2006 11:35 AM
  • 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

    Wednesday, September 13, 2006 10:08 PM
  • 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);
    }

    Thursday, September 14, 2006 3:10 PM
  • I needed this logic in VB, so here is your code written in Visual Basic. 

    (Could not find a similar forum for VB)

        ' Convert Base10 to any other Base (2 to 36)
        ' From msdn.microsoft.com forum C# code - converted to VB

        Public Function convertToBase(ByVal value As Int32, base As Int32) As String

            Dim chars As String = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
            Dim r As Int32
            Dim newNumber As String

            If (base < 2 Or base > chars.Length) Then
                Return ""
            End If

            newNumber = ""

            While value >= base
                r = value Mod base
                newNumber = chars.Substring(r, 1) & newNumber
                value = Convert.ItInt32(Math.Floor(value / base))     ' Don't want to round up
            End While
            If value > 0 Then
                newNumber = chars.Substring(value, 1) & newNumber
            End If

            If String.IsNullOrEmpty(newNumber) Then
                newNumber = "0"
            End If

            r = Nothing
            chars = Nothing

            Return newNumber

        End Function



    • Edited by OS2Dude Friday, August 23, 2013 6:54 PM
    Tuesday, June 11, 2013 5:06 PM
  • That would be the aptly named Visual Basic forum.

    Karl


    When you see answers and helpful posts, please click Vote As Helpful, Propose As Answer, and/or Mark As Answer



    My Blog: http://unlockpowershell.wordpress.com
    My Book: Windows PowerShell 2.0 Bible
    My E-mail: -join ("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})

    Thursday, June 13, 2013 7:29 PM