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. UTF-8 Encoding with File.CreateText

UTF-8 Encoding with File.CreateText

Scheduled Pinned Locked Moved C#
question
5 Posts 3 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.
  • P Offline
    P Offline
    perlmunger
    wrote on last edited by
    #1

    When you use File.CreateText and then pass a normal string to the resulting StreamWriter's Write method, does it automatically convert the text to UTF-8, or do I need to encode it first? Thanks. -Matt ------------------------------------------ The 3 great virtues of a programmer: Laziness, Impatience, and Hubris. --Larry Wall

    D 1 Reply Last reply
    0
    • P perlmunger

      When you use File.CreateText and then pass a normal string to the resulting StreamWriter's Write method, does it automatically convert the text to UTF-8, or do I need to encode it first? Thanks. -Matt ------------------------------------------ The 3 great virtues of a programmer: Laziness, Impatience, and Hubris. --Larry Wall

      D Offline
      D Offline
      Daniel Grunwald
      wrote on last edited by
      #2

      Strings in .NET are an abstract concept, they do not have any encoding but are just unicode (in their internal memory representation, they use UTF-16, but you'll never notice until you use unsafe pointers). Encoding is always done when converting from String to bytes, so yes, the StreamWriter does this for you.

      P 1 Reply Last reply
      0
      • D Daniel Grunwald

        Strings in .NET are an abstract concept, they do not have any encoding but are just unicode (in their internal memory representation, they use UTF-16, but you'll never notice until you use unsafe pointers). Encoding is always done when converting from String to bytes, so yes, the StreamWriter does this for you.

        P Offline
        P Offline
        perlmunger
        wrote on last edited by
        #3

        That's what I thought, but for some reason when I save out my file, it does not seem to be UTF-8 encoded. I know this because when you open a text file in notepad and then select File | Save As..., you will see in the "Encoding" combo box of the Save As dialog, the encoding of the current document. In this case it came up as ASCII. To test this, I saved the file out as the same file name using the UTF-8 encoding with notepad and then closed it and loaded it back into notepad again--once again selecting "Save As..." to see what the Encoding drop down defaults to. It came up as UTF-8 the second time. Is it really writing it out as UTF-8? Any ides? Thanks. -Matt ------------------------------------------ The 3 great virtues of a programmer: Laziness, Impatience, and Hubris. --Larry Wall

        J 1 Reply Last reply
        0
        • P perlmunger

          That's what I thought, but for some reason when I save out my file, it does not seem to be UTF-8 encoded. I know this because when you open a text file in notepad and then select File | Save As..., you will see in the "Encoding" combo box of the Save As dialog, the encoding of the current document. In this case it came up as ASCII. To test this, I saved the file out as the same file name using the UTF-8 encoding with notepad and then closed it and loaded it back into notepad again--once again selecting "Save As..." to see what the Encoding drop down defaults to. It came up as UTF-8 the second time. Is it really writing it out as UTF-8? Any ides? Thanks. -Matt ------------------------------------------ The 3 great virtues of a programmer: Laziness, Impatience, and Hubris. --Larry Wall

          J Offline
          J Offline
          Joe Woodbury
          wrote on last edited by
          #4

          It is writing a UTF-8 stream without any header. Unless you have multi-byte characters, you can't tell the difference between a UTF-8 document and a normal ASCII document. To include the preamble header call: StreamWriter(fileName, System.Text.Encoding.UTF8); Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

          P 1 Reply Last reply
          0
          • J Joe Woodbury

            It is writing a UTF-8 stream without any header. Unless you have multi-byte characters, you can't tell the difference between a UTF-8 document and a normal ASCII document. To include the preamble header call: StreamWriter(fileName, System.Text.Encoding.UTF8); Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

            P Offline
            P Offline
            perlmunger
            wrote on last edited by
            #5

            Suuuhweeet! Thanks. That's what I was looking for. -Matt ------------------------------------------ The 3 great virtues of a programmer: Laziness, Impatience, and Hubris. --Larry Wall

            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