I have problem in c# with convert decimal number from string to byte array. I want creat BigInteger with using byte array.
我在c#中遇到问题,从字符串到字节数组的转换十进制数。我希望创建BigInteger使用字节数组。
I try:
我试试:
string Astr = "123456789123456789123456789123456789123456789123456789123456789123456789123456789";
byte[] AByte = Astr.Select(c => (byte)(c - '0')).ToArray(); //This is problem because array padding wrong.
Tnaks for your ideas. :)
Tnaks为您的想法。 :)
1 个解决方案
#1
1
Why do you need to create the BigInteger from a byte array when you have the string available?
当字符串可用时,为什么需要从字节数组创建BigInteger?
Why not just do this?
为什么不这样做呢?
string aStr = "123456789123456789123456789123456789123456789123456789123456789123456789123456789";
BigInteger x = BigInteger.Parse(aStr);
Also note that there is no easy correspondence between a BigInteger in string form and its byte array.
另请注意,字符串形式的BigInteger与其字节数组之间没有简单的对应关系。
For example, following on from the code above, if you add this:
例如,从上面的代码开始,如果你添加这个:
var ba = x.ToByteArray();
Console.WriteLine(string.Join(" ", ba.Select(v => v.ToString("x"))));
The output is:
输出是:
15 5f 4 84 b6 70 28 c7 73 7b a3 d5 f9 b a1 8 67 12 b0 a5 af 52 ba cb e4 66 6c 75 78 66 92 31 2a 4
Which is the byte[] version of the original string after being encoded as a BigInteger.
在编码为BigInteger之后,哪个是原始字符串的byte []版本。
#1
1
Why do you need to create the BigInteger from a byte array when you have the string available?
当字符串可用时,为什么需要从字节数组创建BigInteger?
Why not just do this?
为什么不这样做呢?
string aStr = "123456789123456789123456789123456789123456789123456789123456789123456789123456789";
BigInteger x = BigInteger.Parse(aStr);
Also note that there is no easy correspondence between a BigInteger in string form and its byte array.
另请注意,字符串形式的BigInteger与其字节数组之间没有简单的对应关系。
For example, following on from the code above, if you add this:
例如,从上面的代码开始,如果你添加这个:
var ba = x.ToByteArray();
Console.WriteLine(string.Join(" ", ba.Select(v => v.ToString("x"))));
The output is:
输出是:
15 5f 4 84 b6 70 28 c7 73 7b a3 d5 f9 b a1 8 67 12 b0 a5 af 52 ba cb e4 66 6c 75 78 66 92 31 2a 4
Which is the byte[] version of the original string after being encoded as a BigInteger.
在编码为BigInteger之后,哪个是原始字符串的byte []版本。