| Unicode | ||
|
Unicode is a multi-language character set designed to encompass virtually all of the characters used with computers today. Unicode characters are represented by a 16-bit value, and differ from other character sets in two important ways. First, unlike the traditional single-byte (ANSI) character sets, Unicode is capable of representing significantly more characters in a variety of languages. Second, unlike multi-byte character sets (where some characters may be one byte in length, while others may be two bytes), the characters are fixed-width, which makes them easier to work with. Whenever a string is assigned to a property value or passed to a method, that string is in Unicode. If necessary, the control will automatically convert that string to ANSI and it does not require any additional programming on the part of the developer. This is all largely transparent when using the components in high-level languages like Visual Basic. However, in Visual C++ and other languages that deal with COM objects on a lower level, it is important to understand that string values must be passed as BSTRs, which are Unicode strings. The issue that most commonly confronts developers with regards to how strings are handled by the SocketTools components are with regards to the Read and Write methods. These methods are used to send and receive data over the network, and accept several different types of data. Typically, the data is exchanged as either a string of text characters, or as an array of bytes. Consider the following code: Dim strMessage As String Dim strBuffer As String Dim cbBuffer As Long Do cbBuffer = SocketWrench1.Read(strBuffer, 1024) If cbBuffer > 0 Then strMessage = strMessage + strBuffer Loop Until cbBuffer < 1 In this case, the program expects to receive data from the server which is textual, and it will be stored in the string strMessage. What happens internally is that the data received from the server is automatically converted from an array of bytes into a string by the control. This is done because the control knows that the strBuffer argument is typed as a String, which means it is Unicode. However, what if the data being returned by the server contains binary data or is already Unicode text? In this case, the data may end up being corrupted because of the conversion performed by the control. To prevent this, the solution is to read the data into an array of bytes rather than a string. For example:
In this case, because the data is being read into a byte array, not a string, then no Unicode conversion is performed and the data is returned exactly as it was sent. Note that Visual Basic also supports the ability to explicitly convert between Unicode strings and byte arrays using the StrConv function. For more information, refer to the language reference and online help in Visual Basic. If you are reading UTF-8 encoded text into a byte array, either from
a file or over a network connection, the FileEncoder control
provides two helper methods:
StrToUtf8 and
Utf8ToStr.
These methods make it easier to convert between UTF-8 stored in a byte
array and the native Unicode If your application needs to display Unicode text in Visual Basic —
for example, an email message written in Greek, Japanese, or another
non-Latin script — you will encounter limitations with the standard
UI controls provided by the Visual Basic runtime. Although Visual Basic
internally supports Unicode strings, the default controls such as
To display Unicode text correctly in Visual Basic 6.0 or other
legacy development environments, regardless of the user's system locale,
you should use Unicode-aware components such as the The Microsoft Forms 2.0 Object Library ( For advanced scenarios, developers familiar with the Windows API
may choose to display Unicode text using functions such as |
||
|
Copyright © 2026 Catalyst Development Corporation. All rights reserved. |