How to open and read a file as a unicode char
-
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....
-
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....
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] }
-
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] }
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....
-
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....
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] }
-
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....
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;
-
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....
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