|
-
Sep 13th, 2009, 10:46 AM
#1
Thread Starter
Member
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:
Dim Hex_Temp As String
Dim Str As String
Dim Tm As Double
Tm = timeGetTime
Hex_Temp = Hex(Tm)
For k = 1 To 8 - Len(Hex_Temp)
Hex_Temp = "0" & Hex_Temp
Next k
Str = Chr(Val("&H" & Mid(Hex_Temp, 7, 2)))
Str = Str & Chr(Val("&H" & Mid(Hex_Temp, 5, 2)))
Str = Str & Chr(Val("&H" & Mid(Hex_Temp, 3, 2)))
Str = Str & Chr(Val("&H" & Mid(Hex_Temp, 1, 2)))
Last edited by iProgrammer; Sep 13th, 2009 at 12:23 PM.
-
Sep 13th, 2009, 04:53 PM
#2
Re: Winsock Hexadecimal Data
Why not use the following. Works for 1,2,4 byte numbers
Code:
Str = Right$("0000000" & Hex(timeGetTime), 8)
-
Sep 14th, 2009, 07:16 AM
#3
Thread Starter
Member
Re: Winsock Hexadecimal Data
 Originally Posted by LaVolpe
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?
-
Sep 14th, 2009, 09:16 AM
#4
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|