[英]C# Convert hex string to decimal . Why negative value?
下面的 HEX -> DEC 代码对于某些值返回负值。
fx HEX:a4ddfc12 应该是:2766011410 但 C# 出于某种原因返回:-1528955886
代码: https : //rextester.com/SHSC23645
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace Rextester
{
public class Program
{
public static void Main(string[] args)
{
string myHexStr = "a4ddfc12";
Console.WriteLine(Int32.Parse(myHexStr,
System.Globalization.NumberStyles.HexNumber).ToString());
}
}
}
如果我使用这个也是一样的: Console.WriteLine(Int32.Parse(Convert.ToInt32(myHexStr, 16).ToString();
预计获得:2766011410
该数字太大,无法放入 32 位整数中,该整数最大为 2147483647,并溢出.
改用 64 位整数:
string myHexStr = "a4ddfc12";
Console.WriteLine(Int64.Parse(myHexStr, NumberStyles.HexNumber).ToString()); // 2766011410
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.