How to Convert Unicode to ANSI Vb

Written by kevin walker
  • Share
  • Tweet
  • Share
  • Email

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


  1. 1

    Open Visual Basic and create a new project. Select "Console Application."

  2. 2

    Paste the following to store some text data in Unicode:

    String s = "Hello. This is a Unicode-encoded string."

  3. 3

    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.

  4. 4

    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.

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.