Example: The float value is -1580.719 and I need to convert it to hex in VB.NET (Value in hex should output: C4C59704).
Asked
Active
Viewed 787 times
0
-
1Hello! Did you try searching StackOverflow for [similar](https://stackoverflow.com/questions/26011546/float-point-to-hex-in-c-sharp) [questions](https://stackoverflow.com/questions/35449339/c-sharp-converting-from-float-to-hexstring-via-ieee-754-and-back-to-float)? I think this has been answered here before. – Sean Skelly May 29 '20 at 23:21
-
Note that `-1580.719` would be encoded as `C4C59702`. The value you gave, i.e., `C4C59704` actually encodes `-1580.7192` as a single-precision IEEE74 floating point number. – alias May 29 '20 at 23:22
-
Thank you both for your comments. I found how to do it! Example below. :D – bosshunter May 29 '20 at 23:24
1 Answers
0
Ok, so I found how to do it:
Dim var As Single = Single.Parse("-1580.719")
Dim varArray() As Byte = BitConverter.GetBytes(var)
Array.Reverse(varArray)
Dim result As String = BitConverter.ToString(varArray).Replace("-", "")
Value of result is:
C4C59702

bosshunter
- 1
- 2
-
You may already be aware but you should accept your own answer so everyone knows that you don't need any more help without opening the question, although I think that you have to wait a while to do so. – jmcilhinney May 30 '20 at 01:56
-
1Yeah, it shows the message: "You can accept your own answer in 2 days". – bosshunter May 30 '20 at 02:19
-
I guess they do that because they figure that someone else may be able to come up with a better answer than the person who needed to ask the question in the first place. – jmcilhinney May 30 '20 at 03:06