Solved

Convert ASCII string to UTF8 string

Posted on 2008-06-17
2
6,026 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Disable extension 8 38
C# bracket error 3 32
.NET 2008 VB and C# 6 28
How do you pull mopre then 1000 records using VB.NET Directory Services namespace 2 21
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
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…
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 …

832 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