0

Which encoding should I use to write bbb to a file as exact bytes, so if the file were opened in a hex editor, its contents would be "99 59"?

The following methods created incorrect results, as listed:

Byte[] bbb = { 0x99, 0x59 };
string o = System.Text.Encoding.UTF32.GetString(bbb);

UTF32 (above) writes 'EF BF BD', UTF7 writes 'C2 99 59', UTF8 writes 'EF BF BD 59', Unicode writes 'E5 A6 99', ASCII writes '3F 59'

What encoding will produce the un-changed 8-bit bytes?

3
  • Why the C++ tag? Its C++/CLI or only C#? Commented Dec 22, 2013 at 13:12
  • 1
    Don't use any encoding. Just write the bytes directly to the file if you don't want them to be changed. Commented Dec 22, 2013 at 13:26
  • Thank you for the answers, in order to follow your 'hard life advice', I had to re-write a few major functions, but the new method is so much cleaner and less messy with work arounds. Commented Dec 23, 2013 at 14:22

1 Answer 1

2

If you want bytes to be written unencoded to a file/stream, simply write them to the file/stream.

File.WriteAllBytes(@"d:\temp\test.bin", bbb);

or

stream.Write(bbb, 0, bbb.Length);

Don't encode them at all.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Lasse, I also used your suggestion and have made some cleaner functions which deal with bytes directly. feedBytes(byteToAddTo, byteArray);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.