Writing hexadecimal to a file
-
I am trying to write a char in hexadecimal format to a binary file. I'm using the following: FileStream fs = new FileStream("test.bin"); BinaryWriter bw = new BinaryWriter(fs); char c = '\xABCD'; bw.Write(c); What I get in the file is EA AF ..... instead of expected AB CD ...... Any ideas? Thx. Samo.
-
I am trying to write a char in hexadecimal format to a binary file. I'm using the following: FileStream fs = new FileStream("test.bin"); BinaryWriter bw = new BinaryWriter(fs); char c = '\xABCD'; bw.Write(c); What I get in the file is EA AF ..... instead of expected AB CD ...... Any ideas? Thx. Samo.
What's happening is that, by default, the character is being written out in UTF-8. If you want to just write out the character as if it were a ushort, either cast it to a ushort, or change the encoding for the binary writer. By the way, writing out the ushort 0xABCD to a file will produce 0xCD 0xAB in that order. Cheers, Julian Program Manager, C# This posting is provided "AS IS" with no warranties, and confers no rights.