本文介绍了将任何基本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
推荐答案
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
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!