Reputation: 932
I am trying to find a way of converting HEX
to a float
in SQL Server.
An example value is 0x42c80000
which corresponds to 100.00
.
It is easy to convert this Hex
value to an Int
using CONVERT
but I can not find the float conversion equivalent.
Upvotes: 5
Views: 3484
Reputation: 2979
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)
Wish I could claim credit, but alas: http://multikoder.blogspot.com.au/2013/03/converting-varbinary-to-float-in-t-sql.html
Also, interesting blog https://blogs.msdn.microsoft.com/psssql/2010/11/01/how-it-works-sql-parsing-of-numbers-numeric-and-float-conversions/
Upvotes: 8