下面的HEX-> DEC代码对于某些值返回负值。
外汇十六进制: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
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句