Been messing with X-Plane UDP packets, and if your a VB6 coder, you will know what a hastle converting a FLOAT Value is. Most of you migrate over to JeffLewis.net and check out his VB6 code, heck, thats what I did. I decided to see if I could optimize his ConvertBytes2Float Function, so for all you VB6 coders replace Jeff's subroutine with this code, I got a 180% increase in speed!! I even made a pure 'c' DLL with this function Exported, still could not beat this in-line code. Here is a screenshot of it in action. Hopefully I gave all the credit where due.
Function ConvertBytesToFloat(byte1 As Byte, byte2 As Byte, byte3 As Byte, byte4 As Byte) As Single '--------------------------------------------------------------- 'This sub converts four bytes to a single precision floating point value 'VB6 has NO built in function for this. Thanks to Jeff Lewis for explaining the Conversion. 'Original code from - > From Jeff Lewis, Jefflewis.net 'I used some optimized Subs from VBspeed Website and a few VB speed tips, and got a 180% increase in speed!! 'Using the VB Speed Template to check speed, went from 120 ticks down to 43 Ticks to convert 50,000 Floats! 'Enjoy - VidHack V.1 Feb 07 '--------------------------------------------------------------- 'CREDITS for the VB Speed Template Tester - NO Code Used, Just used to Test the Optimized Code. '--------------------------------------------------------------- 'Pop-up results, priority, formatting, precision timer, ' commenting, and other stuff added by Spodi. ' http://www.vbgore.com ' 'This program was originated from Almar Joling's template - ' credits go to him for the original idea and design. ' http://www.persistentrealities.com/vbfibre/ '---------------------------------------------------------------- 'CODE STARTS HERE...... Dim float As Single If byte1 = 0 And byte2 = 0 And byte3 = 0 And byte4 = 0 Then 'Convert the float to 0, instead of 5.87747175411144E-39 float = 0 Else Dim NumberString1 As String Dim NumberString2 As String Dim NumberString3 As String Dim NumberString4 As String Dim CombinedString As String Dim BiasedExponent As Integer Dim Mantissa As Double Dim ctr As Integer NumberString1 = ByteToBit11(byte1) NumberString2 = ByteToBit11(byte2) NumberString3 = ByteToBit11(byte3) NumberString4 = ByteToBit11(byte4) 'NOTE: Removed the ENDIAN Reversal option! Since X-Plane 8.50, UDP sent MAC format Only. CombinedString = NumberString1 & NumberString2 & NumberString3 & NumberString4 ' BiasedExponent = 0 For ctr = 2 To 9 If Mid$(CombinedString, ctr, 1) = "1" Then BiasedExponent = BiasedExponent + 2 ^ (9 - ctr) End If Next ctr BiasedExponent = BiasedExponent - 127 Mantissa = 1 For ctr = 10 To 32 If Mid$(CombinedString, ctr, 1) = "1" Then Mantissa = Mantissa + 2 ^ -(ctr - 9) End If Next ctr float = Mantissa * 2 ^ BiasedExponent If Mid$(CombinedString, 1, 1) = "1" Then float = -float End If End If 'float=0 ConvertBytesToFloat = float End Function Public Function ByteToBit10(ByVal B As Long) As String ' by Jost Schwider, jost@schwider.de, 20001228, rev 001 ' based on ByteToBit02 by Donald, donald@xbeat.net, 20001206 ' From VBspeed... ByteToBit10 = "00000000" If B And &H1& Then MidB$(ByteToBit10, 15&) = "1" If B And &H2& Then MidB$(ByteToBit10, 13&) = "1" If B And &H4& Then MidB$(ByteToBit10, 11&) = "1" If B And &H8& Then MidB$(ByteToBit10, 9&) = "1" If B And &H10& Then MidB$(ByteToBit10, 7&) = "1" If B And &H20& Then MidB$(ByteToBit10, 5&) = "1" If B And &H40& Then MidB$(ByteToBit10, 3&) = "1" If B And &H80& Then MidB$(ByteToBit10, 1&) = "1" End Function Public Static Function ByteToBit11(ByVal B As Long) As String ' by Jost Schwider, jost@schwider.de, 20001227 ' From VBspeed... Dim i As Long Dim sByte(0 To 255) As String If i Then ByteToBit11 = sByte(B) Else For i = 0 To 255 sByte(i) = ByteToBit10(i) Next i ByteToBit11 = sByte(B) End If End Function
OK, after POSTING, the CODE was CUT OFF??? So this is useless, Sorry for the POST. Some kind of LIMIT on the CodeBox? Huh?
VidHack
UDP_SCRN_Shot.jpeg (173.96K)
Number of downloads: 74
This post has been edited by VidHack: 28 February 2007 - 06:04 PM

Sign In »
Register Now!
Help
Forum Portal
Forums - Home
Members
Blogs
Calendar
Org Chat
Contact Us


This topic is locked












