Pan Pizza
Pan Pizza

Reputation: 1034

What is the default encoding for a string under VB.NET?

I have a simple question about string. Consider following code:

Dim S1 as String = "abc"

What is the encoding for S1? Is that UTF-8 or depending on user windows local setting?

Upvotes: 7

Views: 9507

Answers (2)

Heinzi
Heinzi

Reputation: 172270

It's an implementation detail you should not need to worry about (unless you leave the Basic Multilingual Plane, in which case things get complicated since Chars represent UTF-16 code units).

When it becomes relevant, i.e., when the string is converted into a byte array, you have to choose the encoding to use:

Dim S1 As String = ...

Dim utf8Bytes = Encoding.UTF8.GetBytes(S1)
Dim utf16Bytes = Encoding.Unicode.GetBytes(S1)

Dim western As New Encoding(1252)
Dim westernBytes = western.GetBytes(S1)

Upvotes: 5

一二三
一二三

Reputation: 21249

System.String is documented to use UTF-16 internally.

Upvotes: 7

Related Questions