HELP ! function to change float to hex

smallzoo

Member
Joined
Apr 29, 2008
Messages
8
Programming Experience
3-5
I have searched the internet the last few days but keep on hitting a brick wall so I am hoping the experts on this forum can help

Simple ( I hope )

I have a string which represents a float ie "1.234"


I need to convert it into a 32bit Hex representation i.e. "0x3F9DF3B6"

( used Floating Point to Hex Converter )


Any one help ?

p.s. I am running Window CE ( compact framework ) if that makes a difference

Thanks
 
Single.Parse to get the single precision float, BitConverter.GetBytes to get the bytes of that, Array.Reverse to reverse the order of the bytes, BitConverter.ToString to get the hex values of that, String.Replace to remove the "-" chars from the string that was produced, add the "0x" prefix to the string and you're done.
 
Thanks for that..

ie where vwrite is the string to write eg 1.2345

Dim number As Single = Single.Parse(vwrite)
Dim BytArr() As Byte = BitConverter.GetBytes(number)
Array.Reverse(BytArr)
Dim rstring As String = BitConverter.ToString(BytArr).Replace("-", "")
BUT is adding the 0x the same as hex(rstring)

Cheers


Single.Parse to get the single precision float, BitConverter.GetBytes to get the bytes of that, Array.Reverse to reverse the order of the bytes, BitConverter.ToString to get the hex values of that, String.Replace to remove the "-" chars from the string that was produced, add the "0x" prefix to the string and you're done.
 
BUT is adding the 0x the same as hex(rstring)
Not sure I understand your question. 0x is a prefix that is used in C style languages, VB uses &H prefix for hex numbers, usually in code for the compiler to understand how to read the following value. Sometimes that is also called a hex specifier, and it is not part of the actual hex value.
 
Back
Top