博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

C# hex和float互转

Posted on 2011-03-01 22:25  hyruur  阅读(2204)  评论(0编辑  收藏  举报
public string FloatToHex(float floatValue)
{
    
uint uintValue = BitConverter.ToUInt32(BitConverter.GetBytes(floatValue), 0);
    
byte[] byteValue = BitConverter.GetBytes(uintValue);
    Array.Reverse(byteValue);
    
return BitConverter.ToString(byteValue).Replace("-","");
}

public float HexToFloat(String hexString)
{
    
uint num = uint.Parse(hexString, System.Globalization.NumberStyles.AllowHexSpecifier);
    
byte[] floatVals = BitConverter.GetBytes(num);
    
return BitConverter.ToSingle(floatVals, 0);
}