Writing Extended ASCII codes with BinaryWriter in .Net

by martin 12. October 2011 16:10
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
  1. String strEscapeCode = new String('\xC4', 5);
  2.  
  3. using (FileStream stream = new FileStream("TestFile.bin", FileMode.Create))
  4. {              
  5.     using (BinaryWriter writer = new BinaryWriter(stream, Encoding.GetEncoding(1252)))
  6.     {
  7.         writer.Write(strEscapeCode.ToCharArray());
  8.         writer.Close();
  9.     }
  10. }

Currently rated 3.0 by 5 people

  • Currently 3/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

C#

Comments

Add comment


(Will show your Gravatar icon)  

biuquote
  • Comment
  • Preview
Loading



Welcome

Please contact me if you have a great idea for a project and need technical expertise in designing, developing, or integrating a custom software solution.

Recent Comments

Comment RSS