Solved

Convert ASCII string to UTF8 string

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

Industry Leaders: 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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

696 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