Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Convert ASCII string to UTF8 string

Posted on 2008-06-17
2
Medium Priority
?
6,393 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 1500 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
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 course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

618 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