如何让我的BigInteger正确看到这个十六进制字符串的二进制表示? [英] How do I make BigInteger see the binary representation of this Hex string correctly?

查看:229
本文介绍了如何让我的BigInteger正确看到这个十六进制字符串的二进制表示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

我有一个字节[] ,能转化为一个十六进制字符串,然后该字符串被解析这样的 BigInteger.Parse(thatString,NumberSyles.Hexnumber)

I have a byte[] that is converted to a hex string, and then that string is parsed like this BigInteger.Parse(thatString,NumberSyles.Hexnumber).

这似乎是浪费,因为BigInteger的是能够接受一个byte [],只要补是占了。

This seems wasteful since BigInteger is able to accept a byte[], as long as the two's complement is accounted for.

的工作(低效率)例如:

根据MSDN 的最后一个字节的最高位显著应是零,以便为以下十六进制数是积极的。下面是有这个问题一个十六进制数的例子:

According to MSDN the most significant bit of the last byte should be zero in order for the following hex number be a positive one. The following is an example of a hex number that has this issue:

byte[] ripeHashNetwork = GetByteHash();
foreach (var item in ripeHashNetwork)
{
   Console.Write(item + "," );
} 

// Output:  
//      0,1,9,102,119,96,6,149,61,85,103,67,158,94,57,248,106,13,39,59,238,214,25,103,246

// Convert to Hex string using this http://stackoverflow.com/a/624379/328397
// Output: 
//       00010966776006953D5567439E5E39F86A0D273BEED61967F6` 

好吧,让我们通过这串入的静态方法的BigInteger

Okay, let's pass that string into the static method of BigInteger:

 BigInteger bi2 = BigInt.Parse(thatString,NumberSyles.Hexnumber);

// Output bi2.ToString() ==
//                {25420294593250030202636073700053352635053786165627414518}

现在,我有数据的基线,而这项工作称为转换,我想使它更好/快/等。

Now that I have a baseline of data, and known conversions that work, I want to make it better/faster/etc.

一个不工作(高效的)例子

现在我的目标是往返一个字节] BigInt有,使结果看起来像 25420294593250030202636073700053352635053786165627414518 。让我们开始吧:

Now my goal is to round-trip a byte[] into BigInt and make the result look like 25420294593250030202636073700053352635053786165627414518. Let's get started:

所以,根据MSDN我在最后一个字节需要一个零,以避免被视为两个的恭维我的电话号码。我会添加零,并打印出来,以确保:

So according to MSDN I need a zero in my last byte to avoid my number from being seen as a two's compliment. I'll add the zero and print it out to be sure:

foreach (var item in ripeHashNetwork)
{
   Console.Write(item + "," );
} 

// Output:                            
//    0,1,9,102,119,96,6,149,61,85,103,67,158,94,57,248,106,13,39,59,238,214,25,103,246,0 

好吧,让我们传递字节[] 的BigInteger 的构造函数:

Okay, let's pass that byte[] into the constructor of BigInteger:

 BigInteger bi2 = new BigInteger(ripeHashNetwork);

// Output bi2.ToString() ==
//                {1546695054495833846267861247985902403343958296074401935327488}

我跳过是什么呢BIGINT我的字节数组,如果我不加尾随零的样本。什么情况是,我得到一个负数这是不对的。我会后,如果你想要的。

What I skipped over is the sample of what bigInt does to my byte array if I don't add the trailing zero. What happens is that I get a negative number which is wrong. I'll post that if you want.

那我做错了吗?

推荐答案

当您通过十六进制字符串下去,你的数组的第一个字节正在成为导致BigInteger的最显著字节。

When you are going via the hex string, the first byte of your array is becoming the most significant byte of the resulting BigInteger.

当你增加一个尾随零,你的数组的最后再见是最显著。

When you are adding a trailing zero, the last bye of your array is the most significant.

我不知道这种情况下,是的右键的你,但是这就是为什么你得到不同的答案。

I'm not sure which case is right for you, but that's why you're getting different answers.

这篇关于如何让我的BigInteger正确看到这个十六进制字符串的二进制表示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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