Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# WriteAllBytes ignores character encoding

I'm using the following code:

File.WriteAllBytes("c:\\test.xml", Encoding.UTF8.GetBytes("THIS IS A TEST"))

Which should in theory write a UTF8 file, but I just get an ANSI file. I also tried this just to be especially verbose;

File.WriteAllBytes("c:\\test.xml", ASCIIEncoding.Convert(ASCIIEncoding.ASCII, UTF8Encoding.UTF8, Encoding.UTF8.GetBytes("THIS IS A TEST")))

Still the same issue though.

I am testing the outputted files by loading in TextPad which reads the format correctly (I tested with a sample file as I know these things can be a bit weird sometimes)

like image 573
Tony Cheetham Avatar asked Apr 10 '18 16:04

Tony Cheetham


Video Answer


1 Answers

WriteAllBytes isn't ignoring the encoding - rather: you already did the encoding, when you called GetBytes. The entire point of WriteAllBytes is that it writes bytes. Bytes don't have an encoding; rather: encoding is the process of converting from text (string here) to bytes (byte[] here).

UTF-8 is identical to ASCII for all ASCII characters - i.e. 0-127. All of "THIS IS A TEST" is pure ASCII, so the UTF-8 and ASCII for that are identical.

like image 193
Marc Gravell Avatar answered Sep 30 '22 00:09

Marc Gravell