将任何基本biginteger转换为base 10并再次转换回base [英] Convert any base biginteger to base 10 and convert back base again

查看:97
本文介绍了将任何基本biginteger转换为base 10并再次转换回base的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望将base 3 biginteger值转换为10,并再次转换base 3。我该怎么办?



我正在使用此代码。但我得到了错误的结果。此代码适用于短值,例如(22222222210222211011111222)



我尝试过:



I want convert base 3 biginteger value to base 10, and convert base 3 again. How can i do?

I am using this code for that. But i am getting wrong result. This code working for short values eg("22222222210222211011111222")

What I have tried:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim BigValue As BigInteger = BigInteger.Parse("22222222210222211011111222011100022001111")
    Dim TenBase As BigInteger = ToTenBase(BigValue, 3)
    Dim TenBaseByte As Byte() = TenBase.ToByteArray
    Dim TreeBase As String = FromTenBase(TenBase, 3)
    If BigValue.ToString <> TreeBase.ToString Then Stop 'Stopping here
End Sub

Function ToTenBase(ByVal BigValue As BigInteger, ByVal Base As BigInteger) As String
    Dim Result As BigInteger = 0
    Dim strValue = BigValue.ToString.ToCharArray
    For i As BigInteger = strValue.Count - 1 To 0 Step -1
        Dim d As BigInteger = strValue(i).ToString
        Dim counter As BigInteger = ((strValue.Count - 1) - i)
        Result = Result + d * (BigInteger.Pow(Base, counter))
    Next
    Return Result.ToString
End Function

Function FromTenBase(BigValue As BigInteger, Base As BigInteger) As String
    Dim Result As String = ""
    Dim Division As BigInteger = BigValue
    Dim Remaining As BigInteger
    Dim Dividing As BigInteger

    Do Until Division < Base
        Dividing = Division
        Division = BigInteger.Divide(Division, Base)
        Remaining = Dividing - Division * Base
        Result = Remaining.ToString & Result
    Loop
    Result = Division.ToString & Result
    Return BigInteger.Parse(Result).ToString
End Function

推荐答案

为什么使用字符串?这是非常浪费和低效的。

相反,使用BigInteger Divide和Modulus操作一次提取一个数字,并将其包含在一个运行总计中:

Why are you using strings? That's very wasteful and inefficient.
Instead, use the BigInteger Divide and Modulus operations to extract each digit one at a time, and include it in a running total:
set powerOf = 1
set total = 0
while input not zero
   extract lowest digit using modulus 10
   multiply digit by powerOf
   add to total
   multiply powerOf by base
   divide input by 10


你的代码是一堆乱七八糟的转换,你需要检查每一个转换按预期工作,使用调试器来执行此操作。

BigValue TenBase 是BigInteger,你想要

TenBase = ToTenBase(BigValue,3)

看看你用ToTenBase做什么

ToTenBase将BigValue作为BigInteger => ; ToString => ToCharArray => ToString => ToBigInteger => ToString => ToBigInteger

建议:你有BigInteger,你想要BigInteger,留BigInteger。

-----

有一个相当简单的方法从数字中提取数字而不转换为字符串

BI = 22222222210222211011111222011100022001111

Un = BI%10 = 1

Tn = BI / 10 = 2222222221022221101111122201110002200111

对你需要的每个数字重复操作。

-----

有一个工具可以让你看到你的代码是什么这样做,它的名字是调试器。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。

当你不明白你的代码在做什么或为什么它做它做的时候,答案就是答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]

Visual Basic / Visual Studio视频教程 - 基本调试 - YouTube [ ^ ]

初学者的Visual Basic .NET编程 - 断点和调试工具 [ ^ ]

调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期效果时,你就接近了一个bug。
Your code is a mess of cascading conversions, you need to check that every conversion works as expected, use the debugger to do so.
BigValue and TenBase are BigInteger, and you want
TenBase = ToTenBase(BigValue, 3)
see what you do with ToTenBase
ToTenBase does BigValue as BigInteger => ToString => ToCharArray => ToString => ToBigInteger => ToString => ToBigInteger
advice: you have BigInteger, you want BigInteger, stay BigInteger.
-----
There is a rather simple way to extract digits from a number without convertion to string
BI = 22222222210222211011111222011100022001111
Un = BI % 10 = 1
Tn = BI / 10 = 2222222221022221101111122201110002200111
repeat operation for each digit you need.
-----
There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]
Visual Basic / Visual Studio Video Tutorial - Basic Debugging - YouTube[^]
Visual Basic .NET programming for Beginners - Breakpoints and Debugging Tools[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


你听说过 Horner的方法 [ ^ ]?它可以消除对数字的影响,你通常不想这样做,因为它很慢。



电话和支票代码是:

Have you heard of Horner's method[^]? It can remove the need to take the power of numbers, and you generally don't want to do that, since it is very slow.

The call and check code is:
string input = "22222222210222211011111222011100022001111";
string Base = "012";
BigInteger base10number = ToBase10Horner(input, Base);
var output = FromBase10(base10number, Base);
var IsTheSameNumber = input.Equals(output.ToString());

我所做的是以下代码:

What I did was the following code:

static string FromBase10(BigInteger value, string BaseChars)
{
    int sign = 1;
    if (value < 0)
    {
        sign = -1;
    }
    else if (value == 0)
    {
        return BaseChars.ToCharArray()[0].ToString();
    }
    value *= sign;

    StringBuilder Result = new StringBuilder();
    int nBase = BaseChars.Length;
    BigInteger Reminder = value;

    do
    {
        BigInteger ModnBase = (Reminder % nBase);
        Result.Append(BaseChars.ToCharArray()[(int)ModnBase]);
        Reminder /= nBase;
    } while (Reminder != 0);
    if (sign == -1)
        Result.Append('-');

    char[] charArray = Result.ToString().ToCharArray();
    Array.Reverse(charArray);
    return new string(charArray);
}

static BigInteger ToBase10Horner(string Value, string BaseChars)
{
    BigInteger nBase = BaseChars.Length;
    int count = Value.Length - 1;
    BigInteger nResult = 0;

    for (int i = 0; i < count; i++)
    {
        nResult = nBase * (nResult + (BigInteger)BaseChars.ToUpper().IndexOf(Value.ToCharArray()[i]));
    }

    nResult += (BigInteger)BaseChars.ToUpper().IndexOf(Value.ToCharArray()[count]);

    return (nResult);
}


这篇关于将任何基本biginteger转换为base 10并再次转换回base的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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