I know that I could get away with File.WriteAllBytes(filepath, bytes), but I would like to keep the C# code as close as possible to the original, java code.
The Java code does something you’re not supposed to do: it’s writing binary data to the standard output, and standard streams aren’t designed for binary data, they’re designed with text in mind. .NET does “the right thing” here and gives you a text interface, not a binary data interface.
The correct method is therefore to write the data to a file directly, not to standard output.
As a workaround you can fake it and convert the bytes to characters using an invariant encoding for the range of Doesn’t work since the “invariant” encoding for .NET strings is UTF-16 which doesn’t accept every byte input as valid; for instance, the byte array byte
:new byte[] { 0xFF }
is an invalid UTF-16 code sequence.