简体   繁体   English

C#将大十进制数转换为字节数组

[英]C# Convert big decimal numbers to byte array

How can I convert a big decimal integer to a byte array. 如何将大十进制整数转换为字节数组。

var number = "969837669069837851043825067021609343597253227631794160042862620526559";

Note that I can't use BigInteger because I'm using Unity under .NET 3.5. 请注意,我无法使用BigInteger因为我在.NET 3.5下使用Unity。

I, personally, would use BigInteger . 我个人将使用BigInteger You can change Unity's scripting equivalent to .NET 4.6 under the player settings, which will give you access to a whole bunch of frameworks previously inaccessible. 您可以在播放器设置下更改与.NET 4.6等效的Unity脚本,这将使您可以访问以前无法访问的大量框架。 According to the documentation .NET 4.6 should contain BigInteger , thus solving your issue. 根据文档, .NET 4.6应该包含BigInteger ,从而解决您的问题。

To change the scripting equivalent, go to Build Settings => Player Settings => Other Settings => Configuration . 要更改等效脚本,请转到Build Settings => Player Settings => Other Settings => Configuration In that list of settings, you should be able to set the script runtime equivalent. 在该设置列表中,您应该能够设置脚本运行时等效项。

Once you've done that, all you have to do convert the number: 完成此操作后,您要做的就是转换数字:

var number = "969837669069837851043825067021609343597253227631794160042862620526559";
byte[] numberBytes = BigInteger.Parse(number).ToByteArray();

You can write your own "BigInteger like type" but I would highly advise against it. 您可以编写自己的“ BigInteger like type”,但我强烈建议您这样做。 It is one of those things were you can do a lot of stuff very wrong very quickly. 这是其中的一件事,您可以很快地做很多非常错误的事情。 And you will never even approach the efficiency of a builtin type like BigInteger. 而且,您甚至永远也不会达到像BigInteger这样的内置类型的效率。

I did write a TryParse replacement for someone stuck on 1.0 however, so I might be able to give you some hints: 我确实写了一个TryParse替代品来代替坚持1.0的人,所以我可能会给你一些提示:

  • use a list of UnsignedIntegers. 使用UnsignedIntegers列表。 Use the biggest integer avalible as the basic type, that way the indexes will stay sanely small. 使用最大的avalable整数作为基本类型,这样索引将保持很小。 And a list because you do not want to deal with growth/shrinking of the integer too 还有一个列表,因为您也不想处理整数的增长/缩小
  • Change into checked context when doing math on any two elements. 对任意两个元素进行数学运算时,请更改为检查的上下文 The .NET Framework can fully detect overflow an underflow errors, but that detection is turned off by default. .NET Framework可以完全检测上溢和下溢错误,但是默认情况下该检测处于关闭状态。 If you got a overflow or underflow error, that means you should increase/decrease the next digit accordingly 如果出现上溢或下溢错误,则意味着您应该相应地增加/减少下一位数字
  • most arythmethic requires at least one temporary variable. 大部分的方法都需要至少一个临时变量。 For addition and substraction, you need to store the overflow/underflow cases. 对于加法和减法,您需要存储上溢/下溢情况。 For Multiplication you need to multiply every array element of ListA with every array Element of ListB. 对于乘法,您需要将ListA的每个数组元素与ListB的每个数组元素相乘。 You just multiply each value ListA with one Element of ListB. 您只需将每个值ListA与ListB的一个元素相乘。 Put it into some temporary variable. 将其放入一些临时变量中。 Then add it to the running sum. 然后将其添加到运行总和。
  • This article on equality/identity checks might help you doing that part right: http://www.codeproject.com/Articles/18714/Comparing-Values-for-Equality-in-NET-Identity-and You want the value type Semantics. 这篇关于平等/身份检查的文章可能会帮助您正确地完成这一部分: http : //www.codeproject.com/Articles/18714/Comparing-Values-for-Equality-in-NET-Identity-,并且您想要值类型语义。
  • Ideally you want to make this type inmutable. 理想情况下,您要使此类型不可变。 Most integrated types and string are (including BigInteger), just so it is easier to work with them in Multitasking/threading scenarios. 大多数集成类型和字符串都是(包括BigInteger),因此在多任务/线程方案中使用它们更容易。
  • You may have to do parsing and toString(). 您可能需要进行解析和toString()。 If so, culture appropirate formating may become a issue quickly. 如果是这样,适当的区域性格式化可能很快就会成为问题。 Most numerical types have Parse() and toString() function that accepts a CultureNumberFormat as parameter. 大多数数值类型都具有接受CultureNumberFormat作为参数的Parse()和toString()函数。 The default overloads simply retreive the thread culture/some other default and use hand it of to their brother. 默认重载只是恢复线程区域性/其他一些默认值,然后将其交给兄弟。

As for the TryParse, here is what I wrote for that case way back: 至于TryParse,这是我为这种情况写的:

//Parse throws ArgumentNull, Format and Overflow Exceptions.
//And they only have Exception as base class in common, but identical handling code (output = 0 and return false).

bool TryParse(string input, out int output){
  try{
    output = int.Parse(input);
  }
  catch (Exception ex){
    if(ex is ArgumentNullException ||
      ex is FormatException ||
      ex is OverflowException){
      //these are the exceptions I am looking for. I will do my thing.
      output = 0;
      return false;
    }
    else{
      //Not the exceptions I expect. Best to just let them go on their way.
      throw;
    }
  }

  //I am pretty sure the Exception replaces the return value in exception case. 
  //So this one will only be returned without any Exceptions, expected or unexpected
  return true;

}

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

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