双字类型十六进制转十进制:
string txt1 = 变量1;
string txt2 = 变量2;
string txt3 = 变量3;
string txt4 = 变量4;
byte[] b = new byte[4];
b[0] = Convert.ToByte(txt1, 16);
b[1] = Convert.ToByte(txt2, 16);
b[2] = Convert.ToByte(txt3, 16);
b[3] = Convert.ToByte(txt4, 16);
decimal d = IEEE754Hex2Decimail(b[0], b[1], b[2], b[3]);
string result1 = d.ToString(); //结果
字类型十六进制转十进制:
string txt5 = 变量1;
string txt6 = 变量2;
byte[] b = new byte[2];
b[0] = Convert.ToByte(txt5, 16);
b[1] = Convert.ToByte(txt6, 16);
int i = IEEE754Hex2Int(b[0], b[1]);
string result2 = i.ToString(); //结果
private decimal IEEE754Hex2Decimail(byte b1, byte b2, byte b3, byte b4)
{
byte[] b = new byte[4];
b[0] = Convert.ToByte(b1.ToString("X2"), 16);
b[1] = Convert.ToByte(b2.ToString("X2"), 16);
b[2] = Convert.ToByte(b3.ToString("X2"), 16);
b[3] = Convert.ToByte(b4.ToString("X2"), 16);
if (BitConverter.IsLittleEndian)
Array.Reverse(b);
decimal d = Convert.ToDecimal(BitConverter.ToSingle(b, 0));
return d;
}
private int IEEE754Hex2Int(byte b1, byte b2)
{
byte[] b = new byte[2];
b[0] = Convert.ToByte(b1.ToString("X2"), 16);
b[1] = Convert.ToByte(b2.ToString("X2"), 16);
if (BitConverter.IsLittleEndian)
Array.Reverse(b);
int f = BitConverter.ToInt16(b, 0);
return f;
}