Click here to Skip to main content
15,887,821 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Good Afternoon

I am trying to read a txt file with the following encoding: &3f9c9aaa

This converts to: 92.35
In the program it uses.

I am trying to read this value and post it on a scoreboard type system.
Any help would be kind

Thanks

What I have tried:

Asking programming friends. Googling stuff like hex, binary and assembly
Posted
Updated 14-Mar-17 9:40am
Comments
OriginalGriff 2-May-16 9:56am    
From one sample?
We can't tell.
What does the source for the other app do with it?
Sergey Alexandrovich Kryukov 2-May-16 9:57am    
This is not "encoding"...
You don't show anything except a little fragment. What would be the point even to look at it?
—SA
Patrice T 2-May-16 10:18am    
Impossible to tell from the single example;
Need more examples, at least.

I wonder if its as simple as (sorry, c#)

C#
string hexString = "43480170";
uint num = uint.Parse(hexString, System.Globalization.NumberStyles.AllowHexSpecifier);

byte[] floatVals = BitConverter.GetBytes(num);
float f = BitConverter.ToSingle(floatVals, 0);
Console.WriteLine("float convert = {0}", f);

// Output: 200.0056   


from How to: Convert Between Hexadecimal Strings and Numeric Types (C# Programming Guide)[^] (4th example down)
 
Share this answer
 
Comments
Jochen Arndt 2-May-16 10:34am    
It is not the hex representation for the single precision value 92.35.
That would be 0x42B8B333 (and 0x3f9c9aaa is 1.22346997).
Captain-Price_ZA 2-May-16 10:59am    
Thanks so much. Any idea what this might be? Please refer to my comment below
Jochen Arndt 2-May-16 11:04am    
I have no idea. I just pointed out that it is not the binary representation of the mentioned single precision floating point value.
Captain-Price_ZA 2-May-16 11:12am    
Okay, Thanks i am still noob but is there any way to convert this to the value because i am trying to make a leaderboard of these values and i need to convert them
Jochen Arndt 2-May-16 11:19am    
You need to know how the value is encoded.
The & character and the used digits indicate that it is a 4 byte hex value. But only the author of the program will know how it is converted.
Hi All

Just to update any future users searching for this issue
I managed to solve it.
its a single precision floating point
IEEE 754

Here is the code i used
this: &433d7bc4
Translates to 189.48346

VB
Private Function ConvertHexToSingle(hexVal As String) As [Single]
    Try
        Dim i As Integer = 0, j As Integer = 0
        Dim bArray As Byte() = New Byte(3) {}
        'Dim new as String =

        For i = 0 To hexVal.Length - 1 Step 2
            bArray(j) = [Byte].Parse(hexVal(i).ToString() + hexVal(i + 1).ToString(), System.Globalization.NumberStyles.HexNumber)
            j += 1
        Next
        Array.Reverse(bArray)
        Dim s As [Single] = BitConverter.ToSingle(bArray, 0)
        Return (s)
    Catch ex As Exception
        Throw New FormatException("The supplied hex value is either empty or in an incorrect format. Use the " + "following format: 00000000", ex)
    End Try
End Function
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900