Valhalla Legends Forums Archive | .NET Platform | [VB] GetDWORD Help

AuthorMessageTime
Spilled[DW]
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

[code]
    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
[/code]
January 7, 2006, 9:33 AM
Myndfyr
Don't use data as strings.  Use byte arrays.
January 7, 2006, 10:42 AM
Spilled[DW]
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 :)
January 7, 2006, 10:55 AM
Spilled[DW]
[quote author=MyndFyre link=topic=13821.msg140934#msg140934 date=1136630566]
Don't use data as strings.  Use byte arrays.
[/quote]

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:

[code]
    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
[/code]

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

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

Packetlog of me echoing 0x25 back:

[code]
3  Hide  Hide  8  Send 
0000  3F 25 08 00 21 3F 3F 3F                            ?%..!???
[/code]

Thanks for the help guys and sorry about the double post.
January 8, 2006, 5:03 AM
Myndfyr
You should also use that actual System.Net.Sockets.Socket class instead of whatever you're using.
January 8, 2006, 9:49 AM
JoeTheOdd
Couldn't he run the string through a "String to Hex" type function, then use the .NET implementation of Integer.parseInt() on it?
January 8, 2006, 11:15 PM
Spilled[DW]
Actually last night I did seem to get it working but would the System.net.Socket class be quicker connection?

Heres the working code:
[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
[/code]
January 8, 2006, 11:32 PM
Myndfyr
[quote author=Joe link=topic=13821.msg141115#msg141115 date=1136762127]
Couldn't he run the string through a "String to Hex" type function, then use the .NET implementation of Integer.parseInt() on it?
[/quote]

But *why* would you do that when you have the CORRECT way to do it?
January 9, 2006, 4:37 AM
Spilled[DW]
[quote author=MyndFyre link=topic=13821.msg141046#msg141046 date=1136713756]
You should also use that actual System.Net.Sockets.Socket class instead of whatever you're using.
[/quote]

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.
January 9, 2006, 6:26 PM
K
[quote author=Spilled[DW] link=topic=13821.msg141212#msg141212 date=1136831183]
How would i convert the Server token (4 bytes) to a long? Thanks in advance! plz no flaming.
[/quote]

This 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!
January 9, 2006, 7:38 PM
Myndfyr
[quote author=Spilled[DW] link=topic=13821.msg141212#msg141212 date=1136831183]
[quote author=MyndFyre link=topic=13821.msg141046#msg141046 date=1136713756]
You should also use that actual System.Net.Sockets.Socket class instead of whatever you're using.
[/quote]

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.
[/quote]

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).
January 9, 2006, 9:34 PM
Spilled[DW]
o i c now. Thanks for your help myndfyre, you too K and Joe

Edit:
Solved.
January 9, 2006, 9:53 PM

Search