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
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,375
Location
Norway
Programming Experience
10+
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.
 

smallzoo

Member
Joined
Apr 29, 2008
Messages
8
Programming Experience
3-5
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.
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,375
Location
Norway
Programming Experience
10+
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.
 

Herman

Well-known member
Joined
Oct 18, 2011
Messages
882
Location
Montreal, QC, CA
Programming Experience
10+
IMO a numerical conversion function should not add the 0x or &H it should be added when you actually use the result of the function.
 
Top Bottom