Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
139 views
in Technique[技术] by (71.8m points)

vb.net - Looking for decimal to alphanumeric number base converter library in Visual Basic (or pseudo code algorithm) WITHOUT recursion

I'm looking for a decimal to alphanumeric number base converter library in Visual Basic that does not use recursion.

I found: http://visualstudiomagazine.com/articles/2010/07/20/when-hexadecimal-is-not-enough.aspx

which includes a demo app but discovered that it uses recursion. The problem with it using recursion became apparent when I attempted to integrate the library into my own Visual Studio Express 2010 Visual Basic project: I got a stack overflow exception.

Now I could consider increasing the size memory allocated for the stack but it might be hard to determine what this would be, given that the recursion depth might vary depending on the value to be converted.

My situation requires a reliable deterministic solution so I would prefer to discount the idea of using recursion.

I shall do more research and endeavour to write the algorithm from scratch but would rather not re-invent the wheel if it already exists so hence this question. A search on here did not quite give me what I was looking for.

Can you point me in the direction of an existing non-recursive decimal to alphanumeric converter library in Visual Basic?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This solution I provide myself appears to work - so simple too! But that's because it is a one-way conversion; the other libraries aim to be two-way conversion, back and forth between difference bases - but I don't need both ways.


Public Class BaseConverter


    Public Shared Function ConvertToBase(num As Integer, nbase As Integer) As String

        Dim retval = ""

        Dim chars As String = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"

        ' check if we can convert to another base
        If (nbase  chars.Length) Then
            retval = ""
        End If

        Dim r As Integer

        Dim newNumber As String = ""

        ' in r we have the offset of the char that was converted to the new base
        While num >= nbase
            r = num Mod nbase
            newNumber = chars(r) & newNumber

            'use: num = Convert.ToInt32(num / nbase)
            '(if available on your system)
            'otherwise:
            num = num  nbase
            ' notice the back slash  - this is integer division, i.e the calculation only reports back the whole number of times that
            ' the divider will go into the number to be divided, e.g. 7  2 produces 3 (contrasted with 7 / 2 produces 3.5, 
            ' float which would cause the compiler to fail the compile with a type mismatch)
        End While

        ' the last number to convert
        newNumber = chars(num) & newNumber

        Return newNumber
    End Function


End Class

I created the above code in Visual Basic based on the C# code in the following link:

CREDIT: http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/5babf71f-4375-40aa-971a-21c1f0b9762b/ ("convert from decimal(base-10) to alphanumeric(base-36)")


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

    // check if we can convert to another base
    if(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;
}

@assylias I couldn't get devx.com/vb2themax/Tip/19316 to work - I got the wrong value back. Thanks though for the suggestion.

There is no evidence that it works. I adjusted some declarations and superficial structure of the code to get it to build successfully in Visual Studio Express 2010 Visual Basic. Then stepped through the code in the Visual Studio Express 2010 Visual Basic debugger, the code is hard to follow: variable names not obvious, no comments as to why it is doing what it is doing. From what I understood it was doing, it did not seem like it should be doing that to do the base conversion.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...