简体   繁体   English

十进制数字字符串到字节数组[c#]

[英]String of decimal numbers to byte array [c#]

I have problem in c# with convert decimal number from string to byte array. 我在C#中遇到问题,将十进制数从字符串转换为字节数组。 I want creat BigInteger with using byte array. 我想使用字节数组创建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. 感谢您的想法。 :) :)

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 []版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM