从隐式无符号十六进制字符串构造BigInteger的正确方法是什么? [英] What is the proper way to construct a BigInteger from an implied unsigned hexadecimal string?

查看:138
本文介绍了从隐式无符号十六进制字符串构造BigInteger的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,因为我有一个隐含的无符号十六进制数字作为字符串(由用户输入提供),需要转换为BigInteger.

I'm running into a problem where as I have an implied unsigned hexadecimal number as a string, provided from user input, that needs to be converted into a BigInteger.

由于BigInteger的带符号性质,因此将最高位设置为(0x8/1000b)时,将任何输入均视为负数.但是,仅通过检查符号位并乘以-1或由于不考虑底层符号的补码而获得绝对值就无法解决此问题.将所有值0xF *都视为-1.

Thanks to the signed nature of a BigInteger any input where the highest order bit is set (0x8 / 1000b) the resulting number is treated as negative. This issue however can't be resolved by simply checking the sign bit and multiplying by -1 or getting the absolute value due to ones's complement which will not respect the underlying notation e.g. treating all values 0xF* as a -1.

以下是一些示例输入/输出

As follows are some example input/output

var style = NumberStyles.HexNumber | NumberStyles.AllowHexSpecifier;


BigInteger.TryParse("6", style) == 6   // 0110 bin
BigInteger.TryParse("8", style) == -8  // 1000 bin
BigInteger.TryParse("9", style) == -7  // 1001 bin
BigInteger.TryParse("A", style) == -6  // 1010 bin
...
BigInteger.TryParse("F", style) == -1  // 1111 bin
...
BigInteger.TryParse("FA", style) == -6 // 1111 1010 bin
BigInteger.TryParse("FF", style) == -1 // 1111 1111 bin
...
BigInteger.TryParse("FFFF", style) == -1 // 1111 1111 1111 1111 bin

从隐式无符号十六进制字符串构造BigInteger的正确方法是什么?

What is the proper way to construct a BigInteger from an implied unsigned hexadecimal string?

推荐答案

为十六进制字符串加上前缀"0"即可:

Prefixing your hex string with a "0" should do it:

BigInteger.TryParse(string.Format("0{0}", "FFFF"), style, ...)

在上面的示例中,我的BigInteger是65535.

My BigInteger is 65535 in the example above.

编辑

BigInteger的摘录文档:

Excerpt from the BigInteger documentation:

解析十六进制字符串时,BigInteger.Parse(String, NumberStyles)和BigInteger.Parse(String,NumberStyles, IFormatProvider)方法假定,如果 字符串中的第一个字节已设置,或者第一个十六进制数字已设置 字符串的代表字节值的低四位, 使用二进制补码表示值.为了 例如,"FF01"和"F01"都代表十进制值-255. 收件人 区分正值和负值,正值应 包括前导零. ToString方法的相关重载, 当它们传递"X"格式的字符串时,请在 返回十六进制字符串以表示正值.

When parsing a hexadecimal string, the BigInteger.Parse(String, NumberStyles) and BigInteger.Parse(String, NumberStyles, IFormatProvider) methods assume that if the most significant bit of the first byte in the string is set, or if the first hexadecimal digit of the string represents the lower four bits of a byte value, the value is represented by using two's complement representation. For example, both "FF01" and "F01" represent the decimal value -255. To differentiate positive from negative values, positive values should include a leading zero. The relevant overloads of the ToString method, when they are passed the "X" format string, add a leading zero to the returned hexadecimal string for positive values.

这篇关于从隐式无符号十六进制字符串构造BigInteger的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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