c# IEEE 754浮点数十六进制转十进制
时间: 2023-09-10 14:14:20 浏览: 244
在C#中,可以使用`BitConverter`类将IEEE 754浮点数的十六进制表示转换为十进制。具体步骤如下:
1. 将十六进制字符串转换为byte数组:
```csharp
string hexString = "40490fdb";
byte[] bytes = Enumerable.Range(0, hexString.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(hexString.Substring(x, 2), 16))
.ToArray();
```
2. 使用`BitConverter`类将byte数组转换为float类型:
```csharp
float floatValue = BitConverter.ToSingle(bytes, 0);
```
3. 将float类型转换为十进制字符串:
```csharp
string decimalString = floatValue.ToString();
```
完整代码如下:
```csharp
string hexString = "40490fdb";
byte[] bytes = Enumerable.Range(0, hexString.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(hexString.Substring(x, 2), 16))
.ToArray();
float floatValue = BitConverter.ToSingle(bytes, 0);
string decimalString = floatValue.ToString();
Console.WriteLine(decimalString);
```
输出结果为:
```
3.141592
```
阅读全文