Solved

Convert ASCII string to UTF8 string

Posted on 2008-06-17
2
6,059 Views
Last Modified: 2013-11-26
Can anyone tell me how to convert an ascii file to the utf-8 format?  I need a solution in VB.NET using the compact framework.   Thanks!
0
Comment
Question by:deegle77
2 Comments
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 21803569
Hi deegle77;

This should work in compact framework. Not sure if you needed to convert it to a string in the program or wanted a new file in utf-8 format, sample code does both.

Fernando
To make a ASCII file into UTF-8 file
 
        ' Set up a stream reader to read the ASCII text file
        Dim sr As New StreamReader("AsciiFile.txt", Encoding.ASCII)
        ' Read the file and convert the ASCII characters to Unicode encoding
        ' by using the ToCharArray to do the encoding
        Dim chrArray() As Char = sr.ReadToEnd().ToCharArray()
        ' Write the data to a new file in UTF-8 encoding.
        Dim sw As New StreamWriter("Utf8File.txt", False, System.Text.Encoding.UTF8)
        sw.Write(chrArray)
        sr.Close()
        sw.Close()
 
To make a ASCII file into UTF-8 String
 
        ' Set up a stream reader to read the ASCII text file
        Dim sr As New StreamReader("AsciiFile.txt", Encoding.ASCII)
        ' Get input file into UTF-8 Byte Array
        Dim byteArray() As Byte = Encoding.UTF8.GetBytes(sr.ReadToEnd())
        ' Convert Byte array to UTF-8 String
        Dim utf8Str As String = Encoding.UTF8.GetString(byteArray)

Open in new window

0
 
LVL 27

Expert Comment

by:VBRocks
ID: 21803702
Just as a note, when create a new instance of a streamreader or streamwriter, it uses UTF-8 encoding by default.

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question