Solved

Convert ASCII string to UTF8 string

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Does this SQL code look like it will work using VS2010 with .NetFramework 4.0 ? 2 42
Tool Box 2 34
vb.net checkbox 7 41
Point to Current Row Ater Refresh Datagridview 3 20
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
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 Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

912 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

16 Experts available now in Live!

Get 1:1 Help Now