Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Switching between Unicode and Ansi data to a socket

Status
Not open for further replies.

Bobofbobland

Programmer
Mar 28, 2003
8
GB
Hi,

I'm using the API to open a socket and send and recieve data.

My code contains the following line and function:

send hTCPSocket, StripToAscii(bytLogin(), Len(strLogin) * 2), Len(strLogin), 0

Private Function StripToAscii(bytUnicode() As Byte, iLength As Integer) As Byte()

Dim iPos As Integer
Dim bytAscii() As Byte
ReDim bytAscii((iLength / 2) - 1)

For iPos = 0 To (iLength / 2) - 1
bytAscii(iPos) = bytUnicode(iPos * 2)
Next
StripToAscii = bytAscii

End Function

Sometimes I send Unicode, sometimes I send the Ascii using the function call above.

I'm reasonably sure that the byte array I pass to the send API call is correct. What I get at the other end though is gibberish - the correct number of bytes but values from Hell - some ascii, some not.

I'm new to VB and sockets and I just wondered if anyone could see anything glaringly wrong with my code.

Any help would be appreciated.
 
Well, I wouldn't send a mix of Unicode and ASCII, myself.

But if you absolutely have to, you need to find a way to indicate to the other end that you're switching from one to another. This can be done by using fixed-length strings so that both ends know when you're switching; or surrounding each data element with a mini-header that indicates the type of data in it's payload.

One other way would be to promote your ASCII values to Unicode, since ASCII by definition is the range of characters 0..127, and Unicode has the exact same characters in those positions. When receiving, you don't care, as all you get is Unicode (presumably UTF-16 encoded)

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top