如何转换十六进制字符串为十进制 [英] How to convert Hex string to Decimal

查看:691
本文介绍了如何转换十六进制字符串为十进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试这一点,

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



我得到一个ArgumentException这样说,

I get an ArgumentException saying this,

数字样式不支持浮点
数据类型AllowHexSpecifier。

The number style AllowHexSpecifier is not supported on floating point data types.

和列[1] = 4B414D000000011613C3 BTW。

and columns[1] = 4B414D000000011613C3 btw.

我究竟做错了,我怎么解决?

what am I doing wrong and how do I fix it ?

推荐答案

小数是浮点类型,如,所以你不能用标准的解析是指像

Decimal is floating point type, like Single, Double and so you can't parse by standard means strings like

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

在另一方面,小数靠近 int128 ,我们不;吨有一个的超级长整型的。如果你的价值的没有那么大的(小于 2 ^ 63 其中约 9.2e18 ),你可以试一下

On the other hand, Decimal is close to int128 and we don;t have that super long int. If your value is not that big (less than 2^63 which about 9.2e18) you can try something

// 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);

在中的超过的该UINT64可以的中开你的价值:

In case of exceeding the UInt64 you can split your value:

// 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);

这篇关于如何转换十六进制字符串为十进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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