Focus on Microsoft Technologies - Tutorials, Articles, Code Samples.

Tuesday, October 03, 2006

.NET Hexadecimal to Floating Point (IEEE 754)

A post just came across the forum I frequent regarding Hexadecimal to Floating Point conversion. Strangely there appears to be no direct way to do this in .NET, and the solutions I found were pretty lame and tedious… so it became my mission to get it done the .NET way, and here is the result: Private Function ConvertHexToSingle(ByVal hexValue As String) As Single Try Dim iInputIndex As Integer = 0 Dim iOutputIndex As Integer = 0 Dim bArray(3) As Byte For iInputIndex = 0 To hexValue.Length - 1 Step 2 bArray(iOutputIndex) = Byte.Parse(hexValue.Chars(iInputIndex) & hexValue.Chars(iInputIndex + 1), Globalization.NumberStyles.HexNumber) iOutputIndex += 1 Next Array.Reverse(bArray) Return BitConverter.ToSingle(bArray, 0) Catch ex As Exception...(read more)

Post a Comment