Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6954749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:38:47+00:00 2026-05-27T14:38:47+00:00

Hello I am creating a simple console application in vb.net in order to convert

  • 0

Hello I am creating a simple console application in vb.net in order to convert a file from any type to utf8 but i can’t figure out how this thing works with the encoding. I know that the source file is in Unicode, but when i convert it to a new format i get junk. Any suggestions? i am not sure if my code is correct

this is my code.

Imports System.IO
Imports System.Text

Module Module1
    Sub Main()
        Console.Write("Please give the filepath (example:c:/tesfile.csv):")
        Dim filepath As String = Console.ReadLine()
        Dim sEncoding As String = DetermineFileType(filepath)
        Dim strContents As String
        Dim strEncodedContents As String
        Dim objReader As StreamReader
        Dim ErrInfo As String
        Dim bString As Byte()
        Try

            'Read the file
            objReader = New StreamReader(filepath)
            'Read untill the end
            strContents = objReader.ReadToEnd()
            'Close The file
            objReader.Close()
            'Write Contents on DOS
            Console.WriteLine(strContents)
            Console.WriteLine("")

            bString = EncodeString(strContents, "UTF-8")
            strEncodedContents = System.Text.Encoding.UTF8.GetString(bString)
            Dim objWriter As New System.IO.StreamWriter(filepath.Replace(".csv", "_encoded.csv"))
            objWriter.WriteLine(strEncodedContents)
            objWriter.Close()
            Console.WriteLine("Encoding Finished")

        Catch Ex As Exception
            ErrInfo = Ex.Message
            Console.WriteLine(ErrInfo)
        End Try        
        Console.ReadKey()
    End Sub

    Public Function DetermineFileType(ByVal aFileName As String) As String
        Dim sEncoding As String = String.Empty

        Dim oSR As New StreamReader(aFileName, True)
        oSR.ReadToEnd()
        ' Add this line to read the file.
        sEncoding = oSR.CurrentEncoding.EncodingName

        Return sEncoding
    End Function

    Function EncodeString(ByRef SourceData As String, ByRef CharSet As String) As Byte()
        'get a byte pointer To the source data
        Dim bSourceData As Byte() = System.Text.Encoding.Unicode.GetBytes(SourceData)

        'get destination encoding 
        Dim OutEncoding As System.Text.Encoding = System.Text.Encoding.GetEncoding(CharSet)

        'Encode the data To destination code page/charset
        Return System.Text.Encoding.Convert(OutEncoding, System.Text.Encoding.UTF8, bSourceData)
    End Function
End Module
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:38:48+00:00Added an answer on May 27, 2026 at 2:38 pm

    StreamReader has a constructor that takes an Encoding if you know the encoding of the file you should pass that into the constructor of StreamReader

    objReader = New StreamReader(filepath, Encoding.UTF32)
    

    EDIT

    You say in a comment that the file is Encoded as UCS-2 from Wikipedia

    The older UCS-2 (2-byte Universal Character Set) is a similar
    character encoding that was superseded by UTF-16 in version 2.0 of the
    Unicode standard in July 1996.2 It produces a fixed-length format by
    simply using the code point as the 16-bit code unit and produces
    exactly the same result as UTF-16 for 96.9% of all the code points in
    the range 0-0xFFFF, including all characters that had been assigned a
    value at that time.

    In which case you can try to decode using UTF-16 which is called Unicode with in System.Text.Encoding so try

    objReader = New StreamReader(filepath, Encoding.Unicode)
    

    FYI
    Unicode is a standard which has a variety of encodings including

    • UTF-8
    • UTF-16 (BigEndian)
    • UTF-16 (LittleEndian)
    • UTF-32 (BigEndian)
    • UTF-32 (LittleEndian)

    For Microsoft to call UTF-16 Unicode is a little misleading but not inaccurate, UTF-16 is one encoding possible for Unicode.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone point me to a really simple hello world type of application that
i'm creating a very simple (hello World quality) web application using spring mvc 3.0.
I was creating a simple web method to access from Java script..But I am
I am creating a simple HTTP client/server application on my local machine but I
Hello I am creating a Windows application (WPF) that is going to be running
Hello can anybody solve this please I'm creating the object in the action class
Hello again ladies and gents! OK, following on from my other question on ASP.NET
Im attempting to use Silverlight and MVC together. After creating a simple Silverlight application
Hello I am sorry to bother you with such a simple question but this
I am trying to go through the process of creating a jar file from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.