Org.BouncyCastle.Math.BigInteger.ToByteArrayUnsigned 的 .NET System.Numerics.BigInteger 等价物是什么? [英] What is the .NET System.Numerics.BigInteger Equivalent of Org.BouncyCastle.Math.BigInteger.ToByteArrayUnsigned?

查看:36
本文介绍了Org.BouncyCastle.Math.BigInteger.ToByteArrayUnsigned 的 .NET System.Numerics.BigInteger 等价物是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用 BouncyCastle 的 .NET 端口,但在转换时遇到了一些问题使用本机 .NET BigInteger 将大整数转换为 System.Guid.

I am currently working with the .NET port of BouncyCastle and I am having some trouble converting a big integer into a System.Guid using the native .NET BigInteger.

对于某些情况,我在一个(源")应用程序中使用 BouncyCastle 将 System.Guid 转换为 Org.BouncyCastle.Math.BigInteger.然后将该值保存为格式 3A2B847A960F0E4A8D49BD62DDB6EB38 的字符串.

For some context, I am using BouncyCastle in one ("source") application to convert a System.Guid to a Org.BouncyCastle.Math.BigInteger. This value is then saved as a string in the format 3A2B847A960F0E4A8D49BD62DDB6EB38.

然后,在另一个(目标")应用程序中,我将保存的 BigInteger 字符串值转换回 System.Guid.

Then, in another ("destination") application, I am taking this saved string value of a BigInteger and am trying to convert it back into a System.Guid.

请注意,在目标应用程序中,我不希望 BouncyCastle 作为参考,而是希望使用核心 .NET 库进行转换.但是,由于我在使用核心 .NET 类进行转换时遇到了问题,因此我正在使用 BouncyCastle 并且以下代码完全符合我的要求:

Please note that in the destination application I do not want BouncyCastle as a reference and would like to use core .NET libraries for the conversion. However, since I am running into problems converting with core .NET classes, I am using BouncyCastle and the following code does exactly what I would like:

var data = "3A2B847A960F0E4A8D49BD62DDB6EB38";
var integer = new Org.BouncyCastle.Math.BigInteger( data, 16 );
var bytes = integer.ToByteArrayUnsigned();
var guid = new Guid( bytes ); // holds expected value: (7A842B3A-0F96-4A0E-8D49-BD62DDB6EB38)

如您所见,Org.BouncyCastle.Math.BigInteger 上有一个 ToByteArrayUnsigned 方法可以实现此功能.如果我在 System.Numerics.BigInteger 上使用 ToByteArray(即使 按照本问题中的讨论调整数组大小)它不起作用,我得到的 System.Guid 与预期不同.

As you can see, there is a ToByteArrayUnsigned method on the Org.BouncyCastle.Math.BigInteger that makes this work. If I use the ToByteArray on the System.Numerics.BigInteger (even when resizing the array as discussed in this question) it does not work and I get a different System.Guid than expected.

那么,使用原生 .NET 类执行与上述操作等效的最佳方法是什么?

So, what is the best way to perform the equivalent to the above operation using native .NET classes?

感谢@John-Tasler 的建议,事实证明这是由于字节序......该死的字节序......你的字节序会结束吗?:P

Thanks to @John-Tasler's suggestion, it turns out this was due to endianess... darn you endianess... will your endiness ever end? :P

<代码>var parsed = System.Numerics.BigInteger.Parse("3A2B847A960F0E4A8D49BD62DDB6EB38", NumberStyles.HexNumber ).ToByteArray();Array.Resize( ref 解析, 16 );var guid = new Guid( parsed.Reverse().ToArray());//万岁!

推荐答案

使用 .NET 的 BigIntegrer 时生成的 Guid 的实际值是多少?

What's the actual value of the resulting Guid when using .NET's BigIntegrer?

可能是这两种实现只是以不同的方式存储字节.字节序等

It could be that the two implementations are just storing the bytes differently. Endianess, etc.

要查看每个实现的字节数组返回的内容:

To see what comes back from each implementation's array of bytes:

var sb = new StringBuilder();

foreach (var b in bytes)
{
    sb.AppendFormat("{0:X2} ", b);
} 
sb.ToString();

这将是一个有趣的比较.

It'd be an interesting comparison.

这篇关于Org.BouncyCastle.Math.BigInteger.ToByteArrayUnsigned 的 .NET System.Numerics.BigInteger 等价物是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆