• Advertisement
Sign in to follow this  

C# Strings and Unicode - Solved

This topic is 3860 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hiya, I'm writing a simple utility in C# to write out unicode files that will be read by a C++ program. I'm using the C# objects String, FileStream and BinaryWriter to do this. I've noticed though, that if the string contains only ASCII characters, it is written out in 8-bit ASCII format, and if it contains Unicode it is written out in UTF-16 format. Can anyone tell me how to force it to use UTF-16, even if no Unicode characters are present? My code is pretty much like this:
try {

    FileStream stream = new FileStream( "test.txt", FileMode.Create, FileAccess.Write);
    BinaryWriter writer = new BinaryWriter(stream);

    string s = txtEntry.Text;    // could be ASCII or Unicode

    writer.Write(s.ToCharArray());

    writer.Close();

}
// catch, etc...
Many thanks for any help [smile] James. Solved - there is an overloaded BinaryWriter constructor that takes an encoding to be used. [Edited by - beebs1 on August 2, 2007 10:48:36 AM]

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement