Converting BinnaryFile to byte[] Russian letters are replaced with?
I create a binary file, then create a byte[] and send it to the server. The server logs that the Russian letters are replaced by ???. Additionally, I tried to save byte[] in file.txt Why can this happen?
BinaryWriter myBinary =
new BinaryWriter(File.Open("C:\\file.dat", FileMode.Create));
myBinary.Write(stream.ToArray());
myBinary.Close();
byte[] byteArray = File.ReadAllBytes(@"C:\\file.dat");
1
1 answers
Use it for conversion:
byte[] bytes = Encoding.ASCII.GetBytes(someString);
Back:
string someString = Encoding.ASCII.GetString(bytes);
For Russian characters, use Encoding.GetEncoding("windows-1251")
(for programs that do not know how to work with unicode and they need to specify the "correct" character code page) or Encoding.UTF8
(for modern programs that understand unicode and in which many encodings are sewn at once)
See. also:
- Converting string to byte array in C# It describes other encodings, not only ASCII-but in in general, the main thing is that the same encoding is used at both ends.
- MetaInt Reading and writing a file - example in Russian
4
Author: A K, 2018-07-26 07:30:21