Jump to content
  • Advertisement
Sign in to follow this  
beebs1

C# Strings and Unicode - Solved

This topic is 4005 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
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!