Ok, so this seems simple enough, but it took me a bit of Googling to figure out that I needed to pass the magic number 1252 to the GetEncoding method to get this to work correctly. ASCIIEncoding only encodes values from 0 to 127 correctly, but values 128 to 255 do not get written out properly. 1252 refers to the Window Code Page for the Latin Alphabet for Western languages, and is sometimes incorrectly referred to as ANSI. Hopefully this will save someone else some time searching for the solution.....
Code Snippet
- String strEscapeCode = new String('\xC4', 5);
-
- using (FileStream stream = new FileStream("TestFile.bin", FileMode.Create))
- {
- using (BinaryWriter writer = new BinaryWriter(stream, Encoding.GetEncoding(1252)))
- {
- writer.Write(strEscapeCode.ToCharArray());
- writer.Close();
- }
- }