在没有递归的情况下在Visual Basic(或伪代码算法)中寻找从十进制到字母数字的基础转换器库 [英] Looking for decimal to alphanumeric number base converter library in Visual Basic (or pseudo code algorithm) WITHOUT recursion

查看:143
本文介绍了在没有递归的情况下在Visual Basic(或伪代码算法)中寻找从十进制到字母数字的基础转换器库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Visual Basic中寻找不使用递归的十进制到字母数字基数转换器库。

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

我发现:
http://visualstudiomagazine.com/articles/2010/07/20/when-hexadecimal-is-not-enough.aspx

包含一个演示应用程序,但发现它使用了递归。当我尝试将库集成到我自己的Visual Studio Express 2010 Visual Basic项目中时,使用递归的问题变得很明显:我遇到了堆栈溢出异常。

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.

能否将我指向Visual中现有的非递归十进制到字母数字转换器库的方向基本吗?

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

推荐答案

我提供给自己的这个解决方案似乎很有效-太简单了!但这是因为它是单向转换;其他库的目标是双向转换,在不同基数之间来回转换-但我不需要两种方法。

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

我基于以下链接中的C#代码在Visual Basic中创建了以上代码:

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

信用: http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/5babf71f-4375-40aa-971a-21c1f0b9762b/
( 从十进制(base-10)转换为字母数字(base-36))

CR 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我找不到 devx.com/vb2themax/Tip/19316 可以正常工作-我找回了错误的值。

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

没有证据表明它有效。我调整了一些声明和代码的表面结构,以使其能够在Visual Studio Express 2010 Visual Basic中成功构建。然后在Visual Studio Express 2010 Visual Basic调试器中逐步执行代码,很难遵循这些代码:变量名称不明显,没有注释其执行的原因。据我了解,它在做基本转换似乎不应该这样做。

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.

这篇关于在没有递归的情况下在Visual Basic(或伪代码算法)中寻找从十进制到字母数字的基础转换器库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆