Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. File and text encoding issue

File and text encoding issue

Scheduled Pinned Locked Moved C#
helptutorialquestion
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Heinz_
    wrote on last edited by
    #1

    Hi, I have the following problem. I create a file using the SaveFileDialog class. The file gets created in ANSI encoding, this according to many programs including notepad and MS Word. But then i try to write strings in non english languages and i get those symbols you know. OK, to try to solve this issue i set the text i want to write to UTF-8 encoding for international character support but the problem is: Have you ever tried to write utf-8 or variants to an ascii file? it's a mess! Is there a way to solve this problem in any way? Creating the file with different encoding (don't know how to do this cause the file gets created in ascii by force) or changing it's encoding type later. Thanx.

    N H 2 Replies Last reply
    0
    • H Heinz_

      Hi, I have the following problem. I create a file using the SaveFileDialog class. The file gets created in ANSI encoding, this according to many programs including notepad and MS Word. But then i try to write strings in non english languages and i get those symbols you know. OK, to try to solve this issue i set the text i want to write to UTF-8 encoding for international character support but the problem is: Have you ever tried to write utf-8 or variants to an ascii file? it's a mess! Is there a way to solve this problem in any way? Creating the file with different encoding (don't know how to do this cause the file gets created in ascii by force) or changing it's encoding type later. Thanx.

      N Offline
      N Offline
      Nader Elshehabi
      wrote on last edited by
      #2

      Hello

      Heinz_ wrote:

      The file gets created in ANSI encoding, this according to many programs including notepad and MS Word.

      Who in the world said that?!?!? You can create your file in any format you want. Besides, if you want to write text in UTF-8, here is a sample:

      SaveFileDialog save = new SaveFileDialog();
      save.ShowDialog();
      StreamWriter Fs = File.CreateText(save.FileName);
      Fs.WriteLine("Test");
      Fs.WriteLine("اختبار");
      Fs.WriteLine("épreuve");
      Fs.WriteLine("Prüfung");
      Fs.WriteLine("试验");
      Fs.WriteLine("試験");
      Fs.Flush();

      Regards:rose:

      H 2 Replies Last reply
      0
      • N Nader Elshehabi

        Hello

        Heinz_ wrote:

        The file gets created in ANSI encoding, this according to many programs including notepad and MS Word.

        Who in the world said that?!?!? You can create your file in any format you want. Besides, if you want to write text in UTF-8, here is a sample:

        SaveFileDialog save = new SaveFileDialog();
        save.ShowDialog();
        StreamWriter Fs = File.CreateText(save.FileName);
        Fs.WriteLine("Test");
        Fs.WriteLine("اختبار");
        Fs.WriteLine("épreuve");
        Fs.WriteLine("Prüfung");
        Fs.WriteLine("试验");
        Fs.WriteLine("試験");
        Fs.Flush();

        Regards:rose:

        H Offline
        H Offline
        Heinz_
        wrote on last edited by
        #3

        Hi, thanx for your answer and your code. I know taht your code works cause i have used this write method tons of time but this time i'm using the Stream class returned by SaveFileDialog.OpenFile(), this write method uses arrays of bytes to write to the files so characters in strings must be converted to bytes corresponding to the encoding type. This is where this encoding type compatibility game begins. I think i'll have to recode this block of code again to use the great StreamWriter, thanks in advance, Heinz

        1 Reply Last reply
        0
        • N Nader Elshehabi

          Hello

          Heinz_ wrote:

          The file gets created in ANSI encoding, this according to many programs including notepad and MS Word.

          Who in the world said that?!?!? You can create your file in any format you want. Besides, if you want to write text in UTF-8, here is a sample:

          SaveFileDialog save = new SaveFileDialog();
          save.ShowDialog();
          StreamWriter Fs = File.CreateText(save.FileName);
          Fs.WriteLine("Test");
          Fs.WriteLine("اختبار");
          Fs.WriteLine("épreuve");
          Fs.WriteLine("Prüfung");
          Fs.WriteLine("试验");
          Fs.WriteLine("試験");
          Fs.Flush();

          Regards:rose:

          H Offline
          H Offline
          Heinz_
          wrote on last edited by
          #4

          Nader Elshehabi wrote:

          Heinz_ wrote: The file gets created in ANSI encoding, this according to many programs including notepad and MS Word. Who in the world said that?!?!?

          Regarding to this, when you call SaveFileDialog.ShowDialog() and the DialogResult is OK, or al least when you call SaveFileDialog.OpenFile(), your file gets created automatically and by force in an ANSI or ASCII formatting, check this info by opening the file with any word editing software that can show you the formatting type info. This is how it works, i didn't say anything, don't blame me.

          1 Reply Last reply
          0
          • H Heinz_

            Hi, I have the following problem. I create a file using the SaveFileDialog class. The file gets created in ANSI encoding, this according to many programs including notepad and MS Word. But then i try to write strings in non english languages and i get those symbols you know. OK, to try to solve this issue i set the text i want to write to UTF-8 encoding for international character support but the problem is: Have you ever tried to write utf-8 or variants to an ascii file? it's a mess! Is there a way to solve this problem in any way? Creating the file with different encoding (don't know how to do this cause the file gets created in ascii by force) or changing it's encoding type later. Thanx.

            H Offline
            H Offline
            Heinz_
            wrote on last edited by
            #5

            I tried the above write method, it works ok, it writes international characters (checked by opening it with notepad) and the encoding type of the file is UTF-8. The file i create and write is a csv. I have another problem now, when i open this file with Excel these special characters are replaced by those weird symbols. Thats an Excel bug but how to solve it? Thanks.

            N 1 Reply Last reply
            0
            • H Heinz_

              I tried the above write method, it works ok, it writes international characters (checked by opening it with notepad) and the encoding type of the file is UTF-8. The file i create and write is a csv. I have another problem now, when i open this file with Excel these special characters are replaced by those weird symbols. Thats an Excel bug but how to solve it? Thanks.

              N Offline
              N Offline
              Nader Elshehabi
              wrote on last edited by
              #6

              Hello For this you have to check the language settings of Excel, or even your windows. Sorry, I don't think I can be of an assistance there. Regards:rose:

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups