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
  1. Home
  2. General Programming
  3. C#
  4. How to write a utf8 string in a unicode text file using c#?

How to write a utf8 string in a unicode text file using c#?

Scheduled Pinned Locked Moved C#
questioncsharphelptutorial
3 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.
  • S Offline
    S Offline
    svt gdwl
    wrote on last edited by
    #1

    Hi I had a string which consists both english and hindi characters. I want to write this in a unicode file, but some junk has been observed in the file. But, if i try to write the same data in utf8 file, it is perfectly written as it is. what is the error? How can i solve this? the code is as follows: string str1 = "This"; string str2 = "यह"; string str3 = str1 + "~" + str2; StreamWriter sw11 = new StreamWriter(File.Open(@"C:\abc.txt", FileMode.Append)); sw11.WriteLine(str3); sw11.Close(); str3 = Encoding.Unicode.GetString(Encoding.Convert(Encoding.ASCII, Encoding.Unicode, Encoding.ASCII.GetBytes(str1))) + Encoding.Unicode.GetString(Encoding.Convert(Encoding.ASCII, Encoding.Unicode, Encoding.ASCII.GetBytes("~"))) + Encoding.Unicode.GetString(Encoding.Convert(Encoding.UTF8, Encoding.Unicode, Encoding.UTF8.GetBytes(str2))); StreamWriter sw12 = new StreamWriter(File.Open(@"C:\abcd.txt", FileMode.Append)); sw12.WriteLine(str3); sw12.Close();

    A L 2 Replies Last reply
    0
    • S svt gdwl

      Hi I had a string which consists both english and hindi characters. I want to write this in a unicode file, but some junk has been observed in the file. But, if i try to write the same data in utf8 file, it is perfectly written as it is. what is the error? How can i solve this? the code is as follows: string str1 = "This"; string str2 = "यह"; string str3 = str1 + "~" + str2; StreamWriter sw11 = new StreamWriter(File.Open(@"C:\abc.txt", FileMode.Append)); sw11.WriteLine(str3); sw11.Close(); str3 = Encoding.Unicode.GetString(Encoding.Convert(Encoding.ASCII, Encoding.Unicode, Encoding.ASCII.GetBytes(str1))) + Encoding.Unicode.GetString(Encoding.Convert(Encoding.ASCII, Encoding.Unicode, Encoding.ASCII.GetBytes("~"))) + Encoding.Unicode.GetString(Encoding.Convert(Encoding.UTF8, Encoding.Unicode, Encoding.UTF8.GetBytes(str2))); StreamWriter sw12 = new StreamWriter(File.Open(@"C:\abcd.txt", FileMode.Append)); sw12.WriteLine(str3); sw12.Close();

      A Offline
      A Offline
      Abhinav S
      wrote on last edited by
      #2

      Maybe this can help you - http://support.microsoft.com/kb/816149.

      My signature "sucks" today

      1 Reply Last reply
      0
      • S svt gdwl

        Hi I had a string which consists both english and hindi characters. I want to write this in a unicode file, but some junk has been observed in the file. But, if i try to write the same data in utf8 file, it is perfectly written as it is. what is the error? How can i solve this? the code is as follows: string str1 = "This"; string str2 = "यह"; string str3 = str1 + "~" + str2; StreamWriter sw11 = new StreamWriter(File.Open(@"C:\abc.txt", FileMode.Append)); sw11.WriteLine(str3); sw11.Close(); str3 = Encoding.Unicode.GetString(Encoding.Convert(Encoding.ASCII, Encoding.Unicode, Encoding.ASCII.GetBytes(str1))) + Encoding.Unicode.GetString(Encoding.Convert(Encoding.ASCII, Encoding.Unicode, Encoding.ASCII.GetBytes("~"))) + Encoding.Unicode.GetString(Encoding.Convert(Encoding.UTF8, Encoding.Unicode, Encoding.UTF8.GetBytes(str2))); StreamWriter sw12 = new StreamWriter(File.Open(@"C:\abcd.txt", FileMode.Append)); sw12.WriteLine(str3); sw12.Close();

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        it is all in the documentation[^], as usual. :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


        I only read formatted code with indentation, so please use PRE tags for code snippets.


        I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


        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