2016-05-30 96 views
2

我正試圖在SQL Server中找到一種將HEX轉換爲float的方法。將十六進制轉換爲浮點SQL

示例值爲0x42c80000,其對應於100.00

使用CONVERT很容易將此Hex值轉換爲Int,但我找不到浮點轉換等效項。

回答

3
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/