Sending raw data in VB question
I have a question about sending raw hex data through a socket.. also a question about interpreting it...
first off.. say i want to send the ascii value 12 through the socket to the client... i want it to be a 4 byte string of hex data, so, when it comes through the other side, it comes out as "00 00 00 0C" i think most of you will understand what I mean.. im using the windows winsock control
also, on receiving a packet, i need to interpret some values into integers...
I know the format of the packet thats arriving.. the format is like this:
LL LL LL LL TT TT TT TT DD DD DD ... DD
where "LL LL LL LL" is a 4 byte hex integer which states the length of the data in the packet (excluding the first 8 bytes)
"TT TT TT TT" is a 4 byte hex integer which is telling me what kind of packet this is (basically, what the data IS (i.e. login information, etc))
and finally "DD DD ... DD" is the data, in hex
when i read the packet im using:
Private Sub socket_DataArrival(Index As Integer, ByVal bytesTotal As Long)
Dim buffer As String
socket(Index).GetData buffer, vbString, bytesTotal
so the data (buffer) is being read as a vbString... next the packet data is sent to my ProcessPacket() function which will be coded to:
extract the length, and the type of packet received and report
so i want it to take the first 4 bytes of my packet and read it as an integer and take the next 4 bytes and read that as an integer also
the question is : HOW do i convert four bytes of data into one integer? this is the part that totally stumps me, and hopefully you guys know what im trying to get at
thanks for any help you can shed!
--RDragon
ICQ: 1094617
email: nicolelia@hotmail.com
1) why are you using a string? use a long!
2) if you really insist on using a string, it becomes ugly... try this tho''
this''ll only work for positive integers though... it gets even uglier with negatives, I''m not exactly sure, but this *should* work:
It *should* work, but as I said I''m not sure and too lazy to try it now. I STRONGLY recommend using a Long to read the data, though.
- JQ
Infiltration: Losing Ground
"Simpson, eh?" -Mr. Burns
"Excellent!" -the above
2) if you really insist on using a string, it becomes ugly... try this tho''
Dim lResult As LonglResult = (Asc(right(buffer,1)))lResult = lResult Or (&H100& * CLng(Asc(Mid(buffer, 3, 1))))lResult = lResult Or (&H10000 * CLng(Asc(Mid(buffer, 2,1))))lResult = lResult Or (&H1000000 * CLng(Asc(Left(buffer,1)) And 127))
this''ll only work for positive integers though... it gets even uglier with negatives, I''m not exactly sure, but this *should* work:
If (Asc(Left(buffer,1)) And 128) Then lResult = lResult Or &H80000000
It *should* work, but as I said I''m not sure and too lazy to try it now. I STRONGLY recommend using a Long to read the data, though.
- JQ
Infiltration: Losing Ground
"Simpson, eh?" -Mr. Burns
"Excellent!" -the above
~phil
im assuming you mean in my .GetData statement to change the vbString to vbLong ? fine, consider it done... i only had it as a string because I didnt know any better alright, so with it as a long, how do i do it now...?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement