Skip to content

Instantly share code, notes, and snippets.

@user202729
Created October 5, 2021 12:43
Show Gist options
  • Save user202729/0374a217610a31c260d7f3bf3b61c9ff to your computer and use it in GitHub Desktop.
Save user202729/0374a217610a31c260d7f3bf3b61c9ff to your computer and use it in GitHub Desktop.
Introduction to Unicode and ANSI string types in VBA

In VBA, there are two types of string: Unicode string and ANSI string.

Definition

Unicode 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

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 ?.

Function to convert between the types

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.

Function to manipulate

Functions such as LeftW manipulates Unicode string.

Functions such as LeftB manipulates ANSI string; however their declared return type is String instead of the actual Byte().

"Double Unicode" string

TODO write something

In some situations it seems to generate the correct result

but it's not very "correct".

External API

TODO write something

Use LongPtr for example. See blog post for more details.

See also

strikethrough text are not verified.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment