Solved

Convert ASCII string to UTF8 string

Posted on 2008-06-17
2
5,934 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 62

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now