简体   繁体   English

从字符串到字节数组C#发送字符

[英]Sending chars from string to byte array C#

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"? 我应该使用哪种编码将bbb作为精确字节写入文件,因此如果文件是在十六进制编辑器中打开的,其内容将是“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' UTF32(上面)写'EF BF BD',UTF7写'C2 99 59',UTF8写'EF BF BD 59',Unicode写'E5 A6 99',ASCII写'3F 59'

What encoding will produce the un-changed 8-bit bytes? 什么编码会产生未更改的8位字节?

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. 根本不要对它们进行编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM