Converting String from one Encoding Type to Another

Last post 05-09-2008 7:38 AM by sukumarraju. 2 replies.

Sort Posts:

  • Converting String from one Encoding Type to Another

    05-09-2008, 6:30 AM
    • Loading...
    • cgi_pro
    • Joined on 01-08-2007, 5:03 AM
    • Posts 46

    Hello

     I have this string

     

    Dim MyString as String = "My Text"
    

     

    I want to convert it to a UTF-8 encoding type or to any other encoding type I specify, how do I do that?

  • Re: Converting String from one Encoding Type to Another

    05-09-2008, 7:15 AM
    • Loading...
    • som.nitk
    • Joined on 01-09-2008, 9:05 AM
    • Posts 99

    use System.Text.Encoding.Convert static method..

    Dont forget to click "Mark as Answer" on the post that helped you.
    This credits the member,earns you a point & marks your thread as Resolved so that new users will know where to search for their queries.
  • Re: Converting String from one Encoding Type to Another

    05-09-2008, 7:38 AM
    Answer
    • Loading...
    • sukumarraju
    • Joined on 06-14-2006, 6:01 PM
    • Scotland, UK
    • Posts 198

    You are looking at Line numbers 11, 12 and 18, 23 and 25.  Please note this code is taken at http://msdn.microsoft.com/en-us/library/system.text.encoding(VS.71).aspx

    Let me know further queries.

    1    Imports System
    2    Imports System.Text
    3    Imports Microsoft.VisualBasic
    4    
    5    Namespace Convert_Example
    6        Class MyConvertExampleClass
    7            Shared Sub Main()
    8                Dim unicodeString As String = "This string contains the unicode character Pi(" & ChrW(&H03A0) & ")"
    9    
    10               ' Create two different encodings.
    11               Dim ascii As Encoding = Encoding.ASCII
    12               Dim [unicode] As Encoding = Encoding.Unicode
    13   
    14               ' Convert the string into a byte[].
    15               Dim unicodeBytes As Byte() = [unicode].GetBytes(unicodeString)
    16   
    17               ' Perform the conversion from one encoding to the other.
    18               Dim asciiBytes As Byte() = Encoding.Convert([unicode], ascii, unicodeBytes)
    19   
    20               ' Convert the new byte[] into a char[] and then into a string.
    21               ' This is a slightly different approach to converting to illustrate
    22               ' the use of GetCharCount/GetChars.
    23               Dim asciiChars(ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)) As Char
    24               ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0)
    25               Dim asciiString As New String(asciiChars)
    26   
    27               ' Display the strings created before and after the conversion.
    28               Console.WriteLine("Original string: {0}", unicodeString)
    29               Console.WriteLine("Ascii converted string: {0}", asciiString)
    30           End Sub
    31       End Class
    32   
    
     
Page 1 of 1 (3 items)