Sign in to follow this  
beebs1

C# Strings and Unicode - Solved

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this