Visual Basic stores all text information by default using the Unicode character set. This allows it to store characters from most known languages (including even some made up languages like Klingon) reliably and accurately. Unicode was developed to solve the confusing mass of code pages that were used to represent the world's languages in ANSI. However, some legacy programs and systems expect their data to be formatted according to ANSI, so Visual Basic provides the tools to do this.
- Skill level:
- Moderately Easy
Other People Are Reading
Open Visual Basic and create a new project. Select "Console Application."
Paste the following to store some text data in Unicode:
String s = "Hello. This is a Unicode-encoded string."
Paste the following to get an ANSI Code Page:
Dim defaultCodePage = Encoding.Default Dim winLatinCodePage = Encoding.GetEncoding(1252)
The first gives you the default code page for the operating system currently being used. So, on a Japanese computer, it will retrieve the default Japanese code page. The second retrieves code page "1252," which is the Windows code page for the Latin (English) alphabet. You can see a full list of supported code page numbers in the first reference.
Paste the following to convert the string to the new encoding:
Dim bytes As Byte() = Encoding.Convert(Encoding.UTF8, winLatinCodePage, s)
Now the string s defined in step 2 has been converted to an array of bytes that holds the ANSI equivalents (as nearly as was possible) according to the ANSI Windows Latin code page.
Tips and warnings
- Unless you have a specific reason for preferring your text to be encoded in ANSI, you should let Visual Basic use Unicode. There are thousands of characters in Unicode that cannot be represented in ANSI, while anything in any of the ANSI code pages can be represented in Unicode. Even relatively common, but nonstandard, characters, like the two accented characters in resume, can cause problems for programs expecting a certain ANSI code page.
- 20 of the funniest online reviews ever
- 14 Biggest lies people tell in online dating sites
- Hilarious things Google thinks you're trying to search for