Valhalla Legends Archive

Programming => General Programming => .NET Platform => Topic started by: Spilled on January 07, 2006, 03:33 AM

Title: [VB] GetDWORD Help
Post by: Spilled on January 07, 2006, 03:33 AM
After reading the 'CopyMemory' topic right below mine, I'm a little lost on how I would do this in .Net? I'm trying to write a getDWORD and getWORD function. How would I go about doing this?

Edit: After reading some would I use the BitConverter.toInt16 for the WORD and .toInt32 for DWORD?

Something like this? It's not working so please take a look and lead me in the correct way. Also please remember, still getting familiar with .Net I always used CopyMemory and trying to learn the correct way. Thanks again everyone


    Public Function GetWORD(ByVal s As String) As Long
        Dim temp(2) As Byte, i As Short
        For i = 1 To temp.Length
            temp(i - 1) = Mid(s, i, 1)
        Next i
        GetWORD = BitConverter.ToInt16(temp, 0)
    End Function
    Public Function GetDWORD(ByVal s As String) As Long
        Dim temp(4) As Byte, i As Short
        For i = 1 To temp.Length
            temp(i - 1) = Mid(s, i, 1)
        Next i
        GetDWORD = BitConverter.ToInt32(temp, 0)
    End Function
Title: Re: [vb] GetDWORD Help
Post by: MyndFyre on January 07, 2006, 04:42 AM
Don't use data as strings.  Use byte arrays.
Title: Re: [vb] GetDWORD Help
Post by: Spilled on January 07, 2006, 04:55 AM
o, I pass the packet to parsep as a String, what do you suggest in VB.Net. A byte array also? Sorry just trying to do this the correct way :)
Title: Re: [vb] GetDWORD Help
Post by: Spilled on January 07, 2006, 11:03 PM
Quote from: MyndFyre on January 07, 2006, 04:42 AM
Don't use data as strings.  Use byte arrays.

Changing from using strings to Byte Array. Im having a problem with the data getting messed up.
On my data Arrival I Split and get the Bytes from the string i recieve. Heres the sub:


    Private Sub wSock_OnDataArrival(ByVal bytesTotal As Integer) Handles wSock.OnDataArrival
        Dim tempBuff As String, pLen As Long
        wSock.GetData(tempBuff)
        pLen = Asc(Mid(tempBuff, 3, 1))
        If pLen < bytesTotal Then
            Dim p1(pLen) As Byte
            Dim p2(bytesTotal - pLen) As Byte
            Array.Copy(Encoding.ASCII.GetBytes(tempBuff), 0, p1, 0, pLen)
            parsep(wSock, p1)
            Array.Copy(Encoding.ASCII.GetBytes(tempBuff), pLen, p2, 0, bytesTotal - pLen)
            parsep(wSock, p2)
        Else
            parsep(wSock, Encoding.ASCII.GetBytes(tempBuff))
        End If
    End Sub


In the parsep I just echo back the 0x25 packet and Its messed up Heres the parsep and a packet log.


    Public Sub parsep(ByVal SOCKET As OSWINSCK.Winsock, ByVal strData() As Byte)
        Select Case strData(1)
            Case &H25
                'SOCKET.SendData(strData)
                MsgBox("0x25")


Packetlog of me echoing 0x25 back:


3  Hide  Hide  8  Send 
0000  3F 25 08 00 21 3F 3F 3F                            ?%..!???


Thanks for the help guys and sorry about the double post.
Title: Re: [vb] GetDWORD Help
Post by: MyndFyre on January 08, 2006, 03:49 AM
You should also use that actual System.Net.Sockets.Socket class instead of whatever you're using.
Title: Re: [vb] GetDWORD Help
Post by: Joe[x86] on January 08, 2006, 05:15 PM
Couldn't he run the string through a "String to Hex" type function, then use the .NET implementation of Integer.parseInt() on it?
Title: Re: [vb] GetDWORD Help
Post by: Spilled on January 08, 2006, 05:32 PM
Actually last night I did seem to get it working but would the System.net.Socket class be quicker connection?

Heres the working code:

        Dim tempBuff(bytesTotal - 1) As Byte, pLen As Long
        wSock.GetData(tempBuff, vbByte + vbArray, bytesTotal) ', vbArray, bytesTotal)
        pLen = tempBuff(2)
        While pLen > 4
            Dim p1(pLen - 1) As Byte
            Array.Copy(tempBuff, 0, p1, 0, pLen)
            parsep(wSock, p1)
            Array.Copy(tempBuff, pLen, tempBuff, 0, tempBuff.Length)
            pLen = tempBuff(2)
        End While
Title: Re: [vb] GetDWORD Help
Post by: MyndFyre on January 08, 2006, 10:37 PM
Quote from: Joe on January 08, 2006, 05:15 PM
Couldn't he run the string through a "String to Hex" type function, then use the .NET implementation of Integer.parseInt() on it?

But *why* would you do that when you have the CORRECT way to do it?
Title: Re: [vb] GetDWORD Help
Post by: Spilled on January 09, 2006, 12:26 PM
Quote from: MyndFyre on January 08, 2006, 03:49 AM
You should also use that actual System.Net.Sockets.Socket class instead of whatever you're using.

What would I Gain from switching to System.net.Sockets? Speed? I'm currently using a dll but I'm capable of switching. Don't mind my question, just interested in learning the best way. After I convert to a byte array, How would i convert the Server token (4 bytes) to a long? Thanks in advance! plz no flaming.
Title: Re: [vb] GetDWORD Help
Post by: K on January 09, 2006, 01:38 PM
Quote from: Spilled[DW] on January 09, 2006, 12:26 PM
How would i convert the Server token (4 bytes) to a long? Thanks in advance! plz no flaming.

This  (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystembitconvertermemberstopic.asp) static class is the answer to all of your questions regarding converting between bytes and integral types.  Learn it. Love it. Use it.



Good luck!
Title: Re: [vb] GetDWORD Help
Post by: MyndFyre on January 09, 2006, 03:34 PM
Quote from: Spilled[DW] on January 09, 2006, 12:26 PM
Quote from: MyndFyre on January 08, 2006, 03:49 AM
You should also use that actual System.Net.Sockets.Socket class instead of whatever you're using.

What would I Gain from switching to System.net.Sockets? Speed? I'm currently using a dll but I'm capable of switching. Don't mind my question, just interested in learning the best way. After I convert to a byte array, How would i convert the Server token (4 bytes) to a long? Thanks in advance! plz no flaming.

You wouldn't; you'd convert it to an integer.  Know the .NET Framework base types (Byte, Short, Integer, Long) and their sizes (1, 2, 4, and 8 bytes respectively).
Title: Re: [vb] GetDWORD Help
Post by: Spilled on January 09, 2006, 03:53 PM
o i c now. Thanks for your help myndfyre, you too K and Joe

Edit:
Solved.