Click here to Skip to main content
15,887,596 members
Home / Discussions / C#
   

C#

 
GeneralRe: Need idea for a neural network... Pin
Alex Korchemniy30-Oct-04 11:01
Alex Korchemniy30-Oct-04 11:01 
GeneralRe: Need idea for a neural network... Pin
tom_dx30-Oct-04 11:17
tom_dx30-Oct-04 11:17 
GeneralRe: Need idea for a neural network... Pin
yoaz31-Oct-04 2:14
yoaz31-Oct-04 2:14 
QuestionHow to get a string's encoding Pin
Matt Gerrans30-Oct-04 9:52
Matt Gerrans30-Oct-04 9:52 
AnswerRe: How to get a string's encoding Pin
Heath Stewart31-Oct-04 9:21
protectorHeath Stewart31-Oct-04 9:21 
GeneralRe: How to get a string's encoding Pin
Matt Gerrans1-Nov-04 5:01
Matt Gerrans1-Nov-04 5:01 
GeneralRe: How to get a string's encoding Pin
Heath Stewart1-Nov-04 5:30
protectorHeath Stewart1-Nov-04 5:30 
GeneralRe: How to get a string's encoding Pin
Matt Gerrans1-Nov-04 11:23
Matt Gerrans1-Nov-04 11:23 
It is incorrect if the file's BOM ("\xff\xfe") says it is Unicode (or UTF16) and the reader thinks it is UTF8 ("\xef\xbb\xbf"). So I call it a bug, because I think it is one. I've since noticed that if I use the StreamReader's string constructor, it correctly identifies it as Unicode, but if I use the FileStream constructor it mis-identifies it as UTF8. So if I do this with a Unicode encoded file:

void SomeMethod( FileInfo info )
{
   StreamReader reader = new StreamReader( info.OpenRead() );
   System.Text.Encoding encoding = reader.CurrentEncoding; // UTF8!?
   string data = reader.ReadToEnd();
   reader.Close();
   
   data = Massage(data);
   
   StreamWriter writer = new StreamWriter( info.OpenWrite(), encoding );
   writer.write(data);
   writer.Close();
}


I get a UTF8 encoded file as a result, which I don't want. On the other hand, if I do this:

void SomeMethod( FileInfo info )
{
   // Use the filename instead of OpenRead()):
   StreamReader reader = new StreamReader( info.FullPath );
   System.Text.Encoding encoding = reader.CurrentEncoding; // Unicode!
   string data = reader.ReadToEnd();
   reader.Close();
   
   data = Massage(data);
   
   StreamWriter writer = new StreamWriter( info.OpenWrite(), encoding );
   writer.write(data);
   writer.Close();
}


The file will be Unicode, as expected (and desired). Maybe the intermediate use of the FileStream causes the loss of the encoding?

Because these files are used by multiple platforms and programming languages (not all of which support MBCS), I want to simply use either ASCII or Unicode, but not UTF8 (or UTF7 or Unicode Big-Endian, etc.). I think there is not that much beauty in UTF8 (the "backward compatibility" also get hosed by use of extended ASCII characters, which usually comes from "backward" text files that were using drawing characters and the like), just unnecessary complexity, especially in these days of multi-gigabyte storage.

By the way, the original question was about detecting the presense of Unicode characters in a string (which, having 16-bit characters could contain some, or not); this would affect the case where the original file was ASCII, but a line with some Unicode characters were inserted into it. In that case, I just want to switch the whole file over to Unicode.

Matt Gerrans
GeneralRe: How to get a string's encoding Pin
Heath Stewart1-Nov-04 11:37
protectorHeath Stewart1-Nov-04 11:37 
GeneralCollection Class's Pin
MrJJKoolJ30-Oct-04 5:24
MrJJKoolJ30-Oct-04 5:24 
GeneralRe: Collection Class's Pin
Colin Angus Mackay31-Oct-04 1:58
Colin Angus Mackay31-Oct-04 1:58 
QuestionHow can i change the Form title bar color Pin
Zapss29-Oct-04 23:56
Zapss29-Oct-04 23:56 
AnswerRe: How can i change the Form title bar color Pin
Heath Stewart30-Oct-04 3:04
protectorHeath Stewart30-Oct-04 3:04 
GeneralUnload Pin
Anonymous29-Oct-04 23:05
Anonymous29-Oct-04 23:05 
GeneralRe: Unload Pin
Stefan Troschuetz30-Oct-04 0:15
Stefan Troschuetz30-Oct-04 0:15 
GeneralRe: Unload Pin
Heath Stewart30-Oct-04 3:19
protectorHeath Stewart30-Oct-04 3:19 
GeneralRe: Unload Pin
Heath Stewart30-Oct-04 3:25
protectorHeath Stewart30-Oct-04 3:25 
Generalconnection through proxy server Pin
Kamran Zafar29-Oct-04 22:35
Kamran Zafar29-Oct-04 22:35 
GeneralRe: connection through proxy server Pin
Heath Stewart30-Oct-04 4:01
protectorHeath Stewart30-Oct-04 4:01 
GeneralMdiWindowListItem in C# express beta Pin
benjymous29-Oct-04 21:04
benjymous29-Oct-04 21:04 
GeneralRe: MdiWindowListItem in C# express beta Pin
Heath Stewart30-Oct-04 3:51
protectorHeath Stewart30-Oct-04 3:51 
GeneralRe: MdiWindowListItem in C# express beta Pin
benjymous30-Oct-04 23:08
benjymous30-Oct-04 23:08 
GeneralRe: MdiWindowListItem in C# express beta Pin
Heath Stewart31-Oct-04 9:19
protectorHeath Stewart31-Oct-04 9:19 
GeneralStrange compile error Pin
steve_rm29-Oct-04 20:14
steve_rm29-Oct-04 20:14 
GeneralRe: Strange compile error Pin
Heath Stewart30-Oct-04 3:47
protectorHeath Stewart30-Oct-04 3:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.