How to get a uniform byte representation of a string in C# without manually specifying encoding?
Hello everyone! I'm new to C# and encountered a problem that puzzled me. I need to transmit strings over the network and hash them, but I noticed that when I convert a string to a byte array, the result can differ on different computers.
\n\nHere's an example of my code:
\nstring message = "Привет, мир!";\nbyte[] bytes = Encoding.Default.GetBytes(message);\n// Sending bytes over the network or computing a hash\n\n\nThe problem is that Encoding.Default gives different results depending on the system locale. On my Windows with Russian locale it works, but on a server with English locale it doesn't. I read that Encoding.ASCII loses characters, and Encoding.Unicode adds a BOM (Byte Order Mark) which messes everything up.
I need to
No answers yet.
Log in to leave an answer
Blogs