Unicode and ASCII may not be encoding correctly

iqworks Information Quality Works 316 Reputation points
2025-03-06T22:35:01.11+00:00

Hi, I am using visual studio 2022 to host a .NET 8 MVC web application project. I am trying to read a text file and write it to another text file.

   This is the code I am using:

string wdata = System.IO.File.ReadAllText(file);          

 // This is the statement that determines the encode
 // result in the output text file

 System.IO.``File.WriteAllText(wfile, wdata, System.Text.Encoding.Unicode);

  The problem is that the text file I am reading from has ”hyphens”. But when I read it, and write it to the other text file, the ”hyphens” turn into:

This is the “hyphen” in the original text I am trying to read.

iqworksmsp encode 0

This is when written using the encoding ASCII

iqworksmsp encode 1

This happens when written with encoding UTF8

iqworksmsp encode 2

Thanks for any advice or suggestions

ASP.NET Core Training
ASP.NET Core Training
ASP.NET Core: A set of technologies in the .NET Framework for building web applications and XML web services.Training: Instruction to develop new skills.
40 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Ping Ni-MSFT 4,890 Reputation points Microsoft External Staff
    2025-03-07T07:26:41.6533333+00:00

    Hi @iqworks Information Quality Works

    Try reading the file using Encoding.UTF8 explicitly to ensure that special characters are preserved:

    string wdata = System.IO.File.ReadAllText(file, System.Text.Encoding.UTF8); System.IO.File.WriteAllText(wfile, wdata, System.Text.Encoding.UTF8);
    

    I would recommend loading the file into a text editor like Notepad++ that tells you what the encoding of the file is (in the status bar). If it's not encoded in UTF-8 to start with, reading and writing as UTF-8 won't work.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Rena


  2. SurferOnWww 3,906 Reputation points
    2025-03-08T01:42:49.93+00:00

    I don't think that your "hyphen" in the text file belongs to ASCII.

    enter image description here

    Please open the text file using binary editor available in Visual Studio 2022.

    enter image description here

    If your "hyphen" really belongs to ASCII it will be shown as "2D":

    enter image description here


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.