How do I convert a hex to a floating point number? as in 0x43F8E354 should be 497.776
This is SQL Server 2008 R2
I've tried http://www.novicksoftware.com/UDFofWeek/Vol1/T-SQL-UDF-Volume-1-Number-23-fn_replinttobitstring.htm, but it does not look as if fn_replinttobitstring exists in Sql Server 2008 R2.
In SSIS I can do like this:
But how to do this in T-SQL?
There is a great discussion of this topic on the main SSC site which includes Jeff Moden, Peter Larsson (whom we know as @Peso on this site) and Martin Wills. Here is the link: http://www.sqlservercentral.com/Forums/Topic356756-8-1.aspx. I just tried the version which Martin Wills posted on page 3 which was in turn a recoding of Peter's code. Here is the paste from the post referenced above:
Here is the test:
Please note that the desired number 497.77600098 is not real (in both senses) because it has way too many digits for a real (a.k.a. float(24)) number to afford. However, if the accurate binary representation 0x43F8E354 of the single floating point number 497.776 is sufficient then the function above should help.
<!-- Begin Edit
Here is the link to Peter's post where he restated both binary to real and binary to float as inline scalar functions: http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=81849. Scroll to the bottom of the post to see them.
End Edit -->
<!-- Begin Edit 2
I am glad that the links helped. However, I would like to comment on the function you use in SSIS. Please don't use it as is if you can because it leaks memory. Not much memory to speak of, but it still does which is never a good thing. Both MemoryStream and BinaryReader are disposable types but the code does not close and dispose any of them. Thus, the code will leak some memory and might eventually trash the server if the function is called frequently enough for a long enough period of time.
Usually the way any conversion works is consistent: it always converts as many bytes as needed. For example if you need to convert a byte array to REAL (System.Single in VB or float in C#) then only 4 bytes of the array will be read and converted. Conversion to FLOAT (System.Double in VB or double in C#) will take to read and then convert 8 bytes of the byte array input. Here is the function written in C# which you can use in place of the one you have (you can use online telerik code converter to convert it to VB from C#):
Similarly, if you need to convert a byte array to a single precision floating point number, you can feed the 4 bytes long byte array to a similar function like this:
As you can see, the code is pretty simple, and does not use any disposable types. Testing the latter using the bytes in your original question:
will print 497.776 in the console window as expected.
End Edit 2 -->