Article ID: 187675 - View products that this article applies to.
This article was previously published under Q187675
This article demonstrates how to copy a string to a byte array without using a Unicode conversion. This technique is useful if you need to pass a string to a function that requires Unicode. The article includes instructions to create a sample project that demonstrates this information.
This article assumes that you know the difference between ANSI and Unicode strings. Although Visual Basic internally converts strings to Unicode for processing, strings are passed to functions as ANSI. Some functions, such as NetUserGetInfo, only accept Unicode strings. If you pass a string from Visual Basic to a function that only accepts Unicode strings, the function will return incomprehensible data. To pass a string to a function that only accepts Unicode strings, you need to convert the string into a byte array.
To convert a string to a byte array, create a dynamic byte array. Set your string variable equal to the byte array. When you loop through the individual elements of the byte array, you will get the Unicode character code for that character
In the case of the English character set, the low-order byte value is the same as the ANSI character code for that character while the high-order byte is a zero.
The next section shows how to create a sample project that demonstrates converting a string in Visual Basic to a byte array.
For more information about ANSI and Unicode, see Chapter 16 "International Issues" in Part 2 of the Visual Basic Programmer's Guide.
Article ID: 187675 - Last Review: June 29, 2004 - Revision: 3.1