# C# Strings and Unicode - Solved

## Recommended Posts

beebs1    398
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]