Now I know this way may not be the most secure/effecient way to do this, but it will turn what would be a multiple hour project to be done in a matter of seconds. If you want to do it all manaully you can, but here is the quick easy way.
Open up a search box, go to modHandleData.
Search for CInt, replace all (in module) with CCInt
Search for CLng, replace all (in module) with CCLng
Search for CByte, replace all (in module) with CCByte
At the bottom add
[code]Private Function CCInt(ByRef s As String) As Integer
' Make sure there is a valid string
If LenB(s) = 0 Then Exit Function
' Check if it's a number
If Not s Like "*[!0-9]*" Then
' Check for overflows
If s >= -32768 Then
If s = -2147483468 Then
If s = 0 Then
If s
You need to do it like this:
Code: Public Function CCInt(ByVal str As String) As Integer
If IsNumeric(str) Then
CCInt = CInt(str)
Else
CCInt = Val(str)
End If
End Function
Public Function CCLng(ByVal str As String) As Integer
If IsNumeric(str) Then
CCLng = CLng(str)
Else
CCLng = Val(str)
End If
End Function
Public Function CCByte(ByVal str As String) As Integer
If IsNumeric(str) Then
CCByte = CCyte(str)
Else
CCByte = Val(str)
End If
End Function
What does Val do that CInt/CByte/CLng don't? Sorry but it seems like if there was any text in it then there would be errors and it wouldn't matter what you changed the number to.
Oh...well I was getting an error with CInt when it was trying to convert "" to 0, so I put a Val on it and it worked.
CInt converts a Numeric string ie "0" , "1", "2" to an integer. "" isn't a number. CCInt will convert that to 0 though. Read the code.
I would make the function more like this
Code: Public Function CCInt(ByRef s As String) As Integer
If Len(s) = 0 Then Exit Function
If Not s Like "*[!0-9]*" Then CCInt = CInt(s)
End Function
* Edit
I changed it a little bit.
I was testing a little bit more..
If you have a string like "2add2", and use Val(string) it will = 2. With the above method it will equal 0.
Usning IsNumeric is probably way faster than your code. But yeah, when I told labmonkey about the IsNumeric function, I suggested to check each packet one by one. May be a lot of work, but looks better.
I did a speed test and mine was a tiny bit faster. I'll post the results in a little bit.
Dugor Wrote:I did a speed test and mine was a tiny bit faster. I'll post the results in a little bit.
How'd you do that?
Testing IsNumeric vs my method. 10000 loops.
Quote:%Faster -40| -25| -25| -25| -25| 33.3| -25| -25| 0| -25
Test1 3| 3| 3| 3| 3| 4| 3| 3| 4| 3
Test2 5| 4| 4| 4| 4| 3| 4| 4| 4| 4
There's only a 1 millisecond difference, but that's still something.
Test1:
Code: Public Sub TestOne()
CCInt "test1"
End Sub
Private Function CCInt(ByRef s As String) As Integer
If LenB(s) = 0 Then Exit Function
If Not s Like "*[!0-9]*" Then CCInt = CInt(s)
End Function
Test2:
Code: Public Sub TestTwo()
CCInt "test2"
End Sub
Private Function CCInt(ByRef s As String) As Integer
If LenB(s) = 0 Then Exit Function
If IsNumeric(s) Then CCInt = CInt(s)
End Function
I use Spodis' speed template for testing: http://www.vbgore.com/Testing_VB_code_speed
Dragoons Master
Unregistered
Convert to binary packets and you'll have no problem with that at all...
Convert to binary packets and you will have to make some free time in your schedule  .
Anyway, in a bit i will edit my tutorial to use Dugor's method. Thank's for posting.
Dragoons Master
Unregistered
Labmonkey Wrote:Convert to binary packets and you will have to make some free time in your schedule .
Anyway, in a bit i will edit my tutorial to use Dugor's method. Thank's for posting. Well, it consumes time, I can't deny, but it's worth the time.
Dragoons Master Wrote:Labmonkey Wrote:Convert to binary packets and you will have to make some free time in your schedule .
Anyway, in a bit i will edit my tutorial to use Dugor's method. Thank's for posting. Well, it consumes time, I can't deny, but it's worth the time.
I dunno. When Dugor helped me convert FPO to byte array packets, it didn't take that long.
Dugor, 1 problem with your test, you only test strings. Trow(
Posts: 2,742
Threads: 115
Joined: Jun 2006
Reputation:
0
[quote="Joost"]Dugor, 1 problem with your test, you only test strings. Trow(
Quote:Robin:
Why aren't maps and shit loaded up in a dynamic array?
Jacob:
the 4 people that know how are lazy
Robin:
Who are those 4 people?
Jacob:
um
you, me, and 2 others?
I figured i didn't need to post the results for just a regular number because the results were about the same.
Quote:%Faster -12.5| -16.7| -28.6| -14.3| 20| -14.3| -14.3| 0| 0| -14.3
Test1 7| 5| 5| 6| 6| 6| 6| 6| 6| 6
Test2 8| 6| 7| 7| 5| 7| 7| 6| 6| 7
Code: Public Sub TestOne()
CCInt "222"
End Sub
Private Function CCInt(ByRef s As String) As Integer
If LenB(s) = 0 Then Exit Function
If Not s Like "*[!0-9]*" Then CCInt = CInt(s)
End Function
Code: Public Sub TestTwo()
CCInt "222"
End Sub
Private Function CCInt(ByRef s As String) As Integer
If LenB(s) = 0 Then Exit Function
If IsNumeric(s) Then CCInt = CInt(s)
End Function
In that case, I give up, the function you use is better than the function I suggested. Proving once again discussion increases general knowledge ;p
Ok I updated the tutorial. Thanks!
Make sure your functions return the right data type!
Code: Private Function CCByte(ByRef s As String) As Integer
Code: Private Function CCByte(ByRef s As String) As Byte
Code: Private Function CCLng(ByRef s As String) As Integer
Code: Private Function CCLng(ByRef s As String) As Long
I was just thinking, if you want to be truely protected with these conversions, you'll also need to check for overflows.
[code]Private Function CCByte(ByRef s As String) As Byte
' Make sure there is a valid string
If LenB(s) = 0 Then Exit Function
' Check if it's a number
If Not s Like "*[!0-9]*" Then
' Check for overflows
If s >= 0 Then
If s = -32768 Then
If s = -2147483468 Then
If s
|