Results 1 to 4 of 4

Thread: Winsock Hexadecimal Data

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2009
    Posts
    38

    Winsock Hexadecimal Data

    I need to send the Following Data.. This is the HEX of the Data I am going to send.

    Code:
    16 00 00 01 XX XX XX XX
    I am using the Following Code.

    Winsock.Sendata chr(22) & chr(0) & chr(0) & chr(1)

    The last 4 bytes should be filled with timeGetTime of my PC. Its a 4 byte value, so i need to send the value in hex bytes.

    How do I write the timeGetTime in Hex format and send it via Winsock?

    I cant use chr(timeGetTime). Is there any other Function that accepts 4 byte value?

    Edit.. I used this Code. It there a better way of doing it? Especially for large numbers
    Hex_Temp Code:
    1. Dim Hex_Temp As String
    2. Dim Str As String
    3. Dim Tm As Double
    4. Tm = timeGetTime
    5. Hex_Temp = Hex(Tm)
    6. For k = 1 To 8 - Len(Hex_Temp)
    7. Hex_Temp = "0" & Hex_Temp
    8. Next k
    9. Str = Chr(Val("&H" & Mid(Hex_Temp, 7, 2)))
    10. Str = Str & Chr(Val("&H" & Mid(Hex_Temp, 5, 2)))
    11. Str = Str & Chr(Val("&H" & Mid(Hex_Temp, 3, 2)))
    12. Str = Str & Chr(Val("&H" & Mid(Hex_Temp, 1, 2)))
    Last edited by iProgrammer; Sep 13th, 2009 at 12:23 PM.

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Winsock Hexadecimal Data

    Why not use the following. Works for 1,2,4 byte numbers
    Code:
    Str = Right$("0000000" & Hex(timeGetTime), 8)
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2009
    Posts
    38

    Re: Winsock Hexadecimal Data

    Quote Originally Posted by LaVolpe View Post
    Why not use the following. Works for 1,2,4 byte numbers
    Code:
    Str = Right$("0000000" & Hex(timeGetTime), 8)
    That is fine.. But look at the code sample above. The Str variable contains string values which represent the timGetTime in String format which i can directly send via Winsock.Senddata. The code that you have given only displays in the Hex Number format. How do i convert into string? I will have to write it the other way around..

    For Example if I need to send 1000 number in Hex data,
    Hex(1000) = 3E8 and when we write it other way around its E8 03. Hence the Packet raw form is Winsock.Senddata chr(232) & chr(03). Its easier for a 2 byte value, but it gets complicated for a 6-8 byte value. Any suggestions?

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Winsock Hexadecimal Data

    Code:
    Str = Right$("0000000" & Hex(timeGetTime), 8)
    Str = Chr$(Asc("&H" & Right$(Str, 2))) & _
            Chr$(Asc("&H" & Mid$(Str, 5, 2))) & _
            Chr$(Asc("&H" & Mid$(Str, 3, 2))) & _
            Chr$(Asc("&H" & Left$(Str, 2)))
    This may be a more generic algo for any byte structure. Note there is no standard variable type that is 6 bytes. 1, 2, 4, 8 is common.
    Code:
    ' add CopyMemory to your declarations section
    Private Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (ByRef Destination As Any, ByRef Source As Any, ByVal Length As Long)
    
    
    ' here is a routine you may be able to use
        Dim bData() As Byte, X As Long, Str As String
        ReDim bData(1 To 4) ' change to nr bytes contained by variable
        CopyMemory bData(1), variable, UBound(bData)
        For X = 1 To UBound(bData)
            Str = Str & Chr$(bData(X))
        Next
    Tweak as needed
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width