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 open and read a file as a unicode char

How to open and read a file as a unicode char

Scheduled Pinned Locked Moved C#
tutorialquestion
6 Posts 4 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.
  • L Offline
    L Offline
    Life as a Coder
    wrote on last edited by
    #1

    How to open a file as a unicode char? I used this : FileStream fs = new FileStream(open.FileName, FileMode.Open,FileAccess.Read); StreamReader sr = new StreamReader(fs, Encoding.Unicode); while (sr.Peek() != -1) { System.Console.WriteLine(sr.Read()); } But somehow it missed some characters. In a file contain: FF D8 FF D8 FF D8 FF E0 00 10 4A 46 49 46 00 01 My code only manage to read: FF E0 00 10 4A 46 49 46 00 01 Anybody have the answer? Thanks before. Training makes perfect....

    L G M 3 Replies Last reply
    0
    • L Life as a Coder

      How to open a file as a unicode char? I used this : FileStream fs = new FileStream(open.FileName, FileMode.Open,FileAccess.Read); StreamReader sr = new StreamReader(fs, Encoding.Unicode); while (sr.Peek() != -1) { System.Console.WriteLine(sr.Read()); } But somehow it missed some characters. In a file contain: FF D8 FF D8 FF D8 FF E0 00 10 4A 46 49 46 00 01 My code only manage to read: FF E0 00 10 4A 46 49 46 00 01 Anybody have the answer? Thanks before. Training makes perfect....

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

      Hi, your file is not a text file, it looks very much like an image file using JFIF format (a special version of JPEG). You can not open it as text; you could open it as a binary file if that is what you really want. Its main purpose is to let you load an image as in Bitmap bm=Bitmap.FromFile("myFileName"); :)

      Luc Pattyn


      try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


      L 1 Reply Last reply
      0
      • L Luc Pattyn

        Hi, your file is not a text file, it looks very much like an image file using JFIF format (a special version of JPEG). You can not open it as text; you could open it as a binary file if that is what you really want. Its main purpose is to let you load an image as in Bitmap bm=Bitmap.FromFile("myFileName"); :)

        Luc Pattyn


        try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


        L Offline
        L Offline
        Life as a Coder
        wrote on last edited by
        #3

        No... I want to open file whatever it is. The main purpose is to open file like UltraEdit32 or somethine like that (hex editor). So it can open every file and read it as a binary. but somehow my program miss some hex like i said before. thanks :)

        Training makes perfect....

        L 1 Reply Last reply
        0
        • L Life as a Coder

          No... I want to open file whatever it is. The main purpose is to open file like UltraEdit32 or somethine like that (hex editor). So it can open every file and read it as a binary. but somehow my program miss some hex like i said before. thanks :)

          Training makes perfect....

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

          If you want to create an app that can read all file types, you must use binary file operations, not text operations. Text operations work for text files only, binary operations work for all file types. So you should use the BinaryReader class, you should not use anything that is text oriented such as: StreamReader.ReadLine() File.ReadAllLines() File.ReadAllText() FileInfo.OpenText() ... that is everything that has "Text" or "Line" in its name, or returns a string or a char or a char[]. To read arbitrary data, you need a byte[]. If you decide the byte[] represents text after all, you can try and decode it using the Text.Encoding class MSDN holds an article "How to: Read and Write to a Newly Created Data File" that should be of interest to you. :)

          Luc Pattyn


          try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


          1 Reply Last reply
          0
          • L Life as a Coder

            How to open a file as a unicode char? I used this : FileStream fs = new FileStream(open.FileName, FileMode.Open,FileAccess.Read); StreamReader sr = new StreamReader(fs, Encoding.Unicode); while (sr.Peek() != -1) { System.Console.WriteLine(sr.Read()); } But somehow it missed some characters. In a file contain: FF D8 FF D8 FF D8 FF E0 00 10 4A 46 49 46 00 01 My code only manage to read: FF E0 00 10 4A 46 49 46 00 01 Anybody have the answer? Thanks before. Training makes perfect....

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #5

            Life as a Coder wrote:

            But somehow it missed some characters.

            No, it didn't. A unicode file starts with a BOM (byte order mark). That is not text, but a code that represents how the file is encoded. -- modified at 11:04 Saturday 4th August, 2007 However, the bytes at the beginning of the file doesn't match any of the existing byte order marks. I believe that Mark has the correct explanation, and that your file simply isn't a valid unicode file.

            --- single minded; short sighted; long gone;

            1 Reply Last reply
            0
            • L Life as a Coder

              How to open a file as a unicode char? I used this : FileStream fs = new FileStream(open.FileName, FileMode.Open,FileAccess.Read); StreamReader sr = new StreamReader(fs, Encoding.Unicode); while (sr.Peek() != -1) { System.Console.WriteLine(sr.Read()); } But somehow it missed some characters. In a file contain: FF D8 FF D8 FF D8 FF E0 00 10 4A 46 49 46 00 01 My code only manage to read: FF E0 00 10 4A 46 49 46 00 01 Anybody have the answer? Thanks before. Training makes perfect....

              M Offline
              M Offline
              Mike Dimmick
              wrote on last edited by
              #6

              0xD8FF is a 'high surrogate' in UTF-16. It represents the first of a two code-unit pair that encodes a character outside the 16-bit Basic Multilingual Plane (anything over U+10000). Because it's not followed by the second code unit of the pair, a low surrogate, it's simply discarded as being illegal. 'Encoding.Unicode' really refers to the 16-bit scheme of encoding Unicode called UTF-16 or UCS-2. See UTF-16/UCS-2[^] on Wikipedia. As the others have said, use a binary stream class, e.g. FileStream, and a method of reading that returns a byte array.

              Stability. What an interesting concept. -- Chris Maunder

              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