数字转换器(以字符串格式大整数) [英] Numeric Converter (big integers in string format)
问题描述
我试图写一个类在C#中,一个大数目(字符串格式)转换为任意数量的系统,我发现这个code在<一个href="http://stackoverflow.com/questions/2652760/how-to-convert-a-gi-normous-integer-in-string-format-to-hex-format-c">How到GI-normous整数(字符串格式)转换为十六进制格式? (C#)
I'm trying to write a class in C# that converts a big number (string format) to any number system, I found this code at How to convert a gi-normous integer (in string format) to hex format? (C#)
var s = "843370923007003347112437570992242323";
var result = new List<byte>();
result.Add( 0 );
foreach ( char c in s )
{
int val = (int)( c - '0' );
for ( int i = 0 ; i < result.Count ; i++ )
{
int digit = result[i] * 10 + val;
result[i] = (byte)( digit & 0x0F );
val = digit >> 4;
}
if ( val != 0 )
result.Add( (byte)val );
}
var hex = "";
foreach ( byte b in result )
hex = "0123456789ABCDEF"[ b ] + hex;
这code也适用于任何数字系统(2 ^ n个基本)与一些修改到code。
This code also works for any numeric system (2^n base) with a few modifications to the code.
现在的问题是,我不知道该算法的逻辑(for语句)。 是否有人可以解释一下这部分的code:
The problem is that I do not understand the logic of the algorithm (the for statement). Can someone please explain this part of the code:
for ( int i = 0 ; i < result.Count ; i++ )
{
int digit = result[i] * 10 + val;
result[i] = (byte)( digit & 0x0F );
val = digit >> 4;
}
if ( val != 0 )
result.Add( (byte)val );
为了使这个code转换,例如,一个字符串十进制为一个字符串的base64,我需要改变的面膜,因此可以计算出六位,而不是仅仅四为十六进制的系统,然后由6右移位到剩余添加到下一个字节。
In order to make this code to convert, for example, a string decimal to a string base64, I need to change the mask so it can calculate six bits, instead of just four for the hex system, and then right-shift digit by 6 to add the remaining to the next byte.
result[i] = (byte)( digit & 0x03F );
val = digit >> 6; // 2^6 = 64
和最后只是改变它的外表表打印结果
and finally just change the look-up table to print the result
hex =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" [ b ] + hex;
这是此行中的for循环的一个我不完全理解
It is this line in the for-loop the one I don't totally understand
int digit = result[i] * 10 + val;
这是这条线,随着循环,这样做对每个迭代结果的每个字节?而且最重要的,为什么呢?
What is this line, along with the loop, doing on each iteration to every byte of result? and most importantly, why?
推荐答案
也许,code是比较容易,如果你比较算法的简化版本,其中的整数可以重新psented在<$ P $明白code> INT 。然后结果
将是一个32位整数,你会初始化为0,并为每一个数字,你会成倍的结果将现有的值
10,并添加下一个数字:
Perhaps the code is easier to understand if you compare the algorithm to the simpler version where the integer can be represented in an int
. Then result
would be a 32 bit integer which you would initialize to 0 and for each digit you would multiply the existing value of result
with 10 and add the next digit:
int result = 0;
foreach ( char c in s ) {
int val = (int)( c - '0' );
result = 10*result + val;
}
有关大整数,你需要一个字节数组重新present要了解整数和循环是一种用10乘以未知长度的字节数组中的价值,同时还加入了数字值。由于乘以10不是一个简单的移位你需要额外的code。
For big integers you need a byte array to represent the integer and the loop that you want to understand is a way to multiply the value in the byte array of unknown length with 10 while also adding the digit value. Because multiplying by 10 is not a simple bit shift you need that extra code.
这篇关于数字转换器(以字符串格式大整数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!