Solved

Convert ASCII string to UTF8 string

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

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

Suggested Solutions

Title # Comments Views Activity
how to remove error in database 6 51
VB.Net Data Class 1 42
.NET universe documentation poster 2 16
Need Help With GDI+ in VB.Net 8 33
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

739 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