In VBA, there are two types of string: Unicode string and ANSI string.
Unicode string (type String
) contains a sequence of UTF-16 code points, and can represent any Unicode characters.
Characters not in the BMP (Basic Multilingual Plane) is represented by a pair of surrogate.
Functions such as ChrW
, AscW
handles
ANSI string (type Byte()
) contains a sequence of bytes, which is interpreted as a sequence of characters in the current locale.
The locale ID for English (United States) is 1033.
It's not possible to represent all Unicode characters in an ANSI string. Characters that cannot be represented is converted to .?
There's a function StrConv
. As mentioned in the documentation:
bytearray = StrConv(string, vbFromUnicode[, locale])
converts from a Unicode string to an ANSI string.string = StrConv(bytearray, vbUnicode[, locale])
converts from an ANSI string to a Unicode string.
Note that even though the input and output type of StrConv
is always String
, they must be taken to be String
or Byte()
depends on the conversion type.
In VBA, String
and Byte()
are interchangeable (but not the same type).
It's also possible to assign string to bytearray, in which case the content of the byte array is assigned the internal representation of the string (UTF-16-LE).
Each UTF-16 code unit (not necessary a character) is represented by 2 bytes.
Functions such as manipulates Unicode string.LeftW
Functions such as LeftB
manipulates ANSI string; however their declared return type is String
instead of the actual Byte()
.
TODO write something
In some situations it seems to generate the correct result
- vba - Split string into array of characters? - Stack Overflow
- vba - How can I create text files with special characters in their filenames - Stack Overflow
but it's not very "correct".
TODO write something
Use LongPtr
for example. See blog post for more details.
- vb6 - What does vbFromUnicode mean? - Stack Overflow
- excel - How to convert a simple string to Byte Array in VBA? - Stack Overflow
strikethrough text are not verified.