简体   繁体   English

如何将十六进制字符串转换为十进制

[英]How to convert Hex string to Decimal

When I try this, 当我尝试这个

Decimal m = Decimal.Parse(columns[1], System.Globalization.NumberStyles.AllowHexSpecifier);

I get an ArgumentException saying this, 我得到一个ArgumentException这样说,

The number style AllowHexSpecifier is not supported on floating point data types. 浮点数据类型不支持数字样式AllowHexSpecifier。

and columns[1] = 4B414D000000011613C3 btw. 和column [1] = 4B414D000000011613C3 btw。

what am I doing wrong and how do I fix it ? 我在做什么错以及如何解决?

Decimal is a floating point type. Decimal是浮点类型。 Try using int.Parse instead. 尝试改用int.Parse

Decimal is floating point type, like Single , Double and so you can't parse by standard means strings like Decimal是浮点类型,例如SingleDouble ,因此您不能通过标准方式解析字符串,例如

  4B414.D000000011613C3eAF // <- '.' decimal point; 'e' exponent sign  

On the other hand, Decimal is close to int128 and we don;t have that super long int . 另一方面, Decimal接近int128而我们没有那么长的int If your value is not that big (less than 2^63 which about 9.2e18 ) you can try something 如果您的值不是那么大 (小于2 ^ 63 ,即9.2e18 ),您可以尝试一下

// It's OK in your case:
// 4B414D000000011613C3 (hex) = 5422700088726126870 (dec) < 9223372036854775808 
// use long.Parse() or ulong.Parse(): int is too small
Decimal result = long.Parse(columns[1], System.Globalization.NumberStyles.HexNumber);

In case of exceeding the UInt64 you can split your value: 如果超出 UInt64,则可以分割您的值:

// to simplify the idea, I remove negative values support
String source = "4B414D000000011613C3";

String highPart = source.Remove(source.Length - 16);
String lowPart = source.Substring(source.Length - 16);

Decimal result =
  ulong.Parse(highPart, System.Globalization.NumberStyles.HexNumber);

result = result * ulong.MaxValue + ulong.Parse(lowPart,  System.Globalization.NumberStyles.HexNumber);

You have a 20 character string representing a hexadecimal integer and you want to convert it a numeric format. 您有一个代表十六进制整数的20字符串,并且想要将其转换为数字格式。 20 characters is 80 bits so it won't fit into an integer but it will fit into a Decimal. 20个字符为80位,因此它不能容纳整数,但可以容纳十进制。 Decimal is a 128-bit floating point representation with 96 bits of mantissa. 十进制是具有96位尾数的128位浮点表示形式。 There is no built-in conversion function that can do this. 没有内置的转换功能可以做到这一点。

The best strategy I can think of is this. 我能想到的最佳策略就是这个。

  1. Break the string into 3 parts, starting from the right, taking 8 characters each time. 从右开始将字符串分成3个部分,每次取8个字符。 The strings will be HI=4, MI=8 and LO=8 characters. 字符串将为HI = 4,MI = 8和LO = 8个字符。
  2. Convert each sub-string into an integer using Parse and the hex specifier. 使用Parse和十六进制说明符将每个子字符串转换为整数。
  3. Combine the parts using the Decimal ctor: Decimal(LO,MI,HI,0,0). 使用十进制ctor组合零件:Decimal(LO,MI,HI,0,0)。

See http://msdn.microsoft.com/en-us/library/bb1c1a6x%28v=vs.110%29.aspx for details. 有关详细信息,请参见http://msdn.microsoft.com/zh-cn/library/bb1c1a6x%28v=vs.110%29.aspx

This is based on the msdn example where in the Hex value will be converted to doubles. 这基于msdn示例,其中十六进制值将转换为双精度。 And the same values has been taken to varify the results. 并采用了相同的值来验证结果。

        `double dnumber = 0;

        long number;
        bool result = Int64.TryParse(hexString,
                NumberStyles.HexNumber, CultureInfo.InvariantCulture,
                out number);
        dnumber=BitConverter.Int64BitsToDouble(number);

        return dnumber;`

To use this I have given sample working program, use it in a console application and check the results. 要使用此程序,我给出了示例工作程序,请在控制台应用程序中使用它并检查结果。

`static void Main(string[] args) { const string formatter = "{0,27:E16}"; `static void Main(string [] args){const string formatter =“ {0,27:E16}”;

        Console.WriteLine(string.Format(formatter, HexStringToDouble("0")));
        Console.WriteLine(string.Format(formatter, HexStringToDouble("3FF0000000000000")));
        Console.WriteLine(string.Format(formatter, HexStringToDouble("402E000000000000")));
        Console.WriteLine(string.Format(formatter, HexStringToDouble("406FE00000000000")));
        Console.WriteLine(string.Format(formatter, HexStringToDouble("41EFFFFFFFE00000")));
        Console.WriteLine(string.Format(formatter, HexStringToDouble("3F70000000000000")));
        Console.WriteLine(string.Format(formatter, HexStringToDouble("3DF0000000000000")));
        Console.WriteLine(string.Format(formatter, HexStringToDouble("0000000000000001")));
        Console.WriteLine(string.Format(formatter, HexStringToDouble("000000000000FFFF")));
        Console.WriteLine(string.Format(formatter, HexStringToDouble("0000FFFFFFFFFFFF")));
        Console.WriteLine(string.Format(formatter, HexStringToDouble("FFFFFFFFFFFFFFFF")));
        Console.WriteLine(string.Format(formatter, HexStringToDouble("FFF0000000000000")));
        Console.WriteLine(string.Format(formatter, HexStringToDouble("7FF0000000000000")));
        Console.WriteLine(string.Format(formatter, HexStringToDouble("FFEFFFFFFFFFFFFF")));
        Console.WriteLine(string.Format(formatter, HexStringToDouble("7FEFFFFFFFFFFFFF")));
        Console.WriteLine(string.Format(formatter, HexStringToDouble(long.MinValue.ToString())));
        Console.WriteLine(string.Format(formatter, HexStringToDouble(long.MaxValue.ToString())));

        Console.ReadKey();
    }

    private static double HexStringToDouble(string hexString)
    {
        double dnumber = 0;

        long number;
        bool result = Int64.TryParse(hexString,
                NumberStyles.HexNumber, CultureInfo.InvariantCulture,
                out number);
        dnumber=BitConverter.Int64BitsToDouble(number);

        return dnumber;
    }`

The output will be 输出将是

在此处输入图片说明

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

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