将Hex转换为Float SQL

时间:2016-05-30 10:52:31

标签: sql sql-server floating-point hex

我正在尝试找到一种在SQL Server中将HEX转换为float的方法。

示例值为0x42c80000,对应100.00

使用Hex很容易将此Int值转换为CONVERT但我无法找到等效的浮动转换。

1 个答案:

答案 0 :(得分:4)

DECLARE @BinaryFloat AS VARBINARY(4); 
SET @BinaryFloat = CONVERT(VARBINARY, '0x42c80000',1);

SELECT SIGN(CAST(@BinaryFloat AS INT))
  * (1.0 + (CAST(@BinaryFloat AS INT) &  0x007FFFFF) * POWER(CAST(2 AS REAL), -23))
  * POWER(CAST(2 AS REAL), (CAST(@BinaryFloat AS INT) & 0x7f800000) / 0x00800000 - 127)

希望我可以申请信用,但唉:http://multikoder.blogspot.com.au/2013/03/converting-varbinary-to-float-in-t-sql.html

此外,有趣的博客https://blogs.msdn.microsoft.com/psssql/2010/11/01/how-it-works-sql-parsing-of-numbers-numeric-and-float-conversions/