用C#将扩展的ASCII字符写入字符串 [英] Write Extended ASCII Char Into String in C#

查看:81
本文介绍了用C#将扩展的ASCII字符写入字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我想将byte []转换为字符串,它可以在以下行中正常工作.

Hello All,

I want to convert byte[] into string, it working correctly with following line.

byte[] byArr= new byte[10];

string ascii =  Encoding.ASCII.GetString(byArr);



但是当char的int值大于127(即扩展的ASCII char)时,与ASCII字符相比,我得到的字符字符串错误.我尝试了许多编码器类,但是每次获得不同的值时,即与ASCII表中的值不同.

请帮助我解决此问题.



But when the char''s int value is greater than 127 (i.e extended ASCII char), then i am getting string with wrong character as compare to ASCII characters. I tried with many encoder classes, but each time i am getting different values i.e. which is not same as values in the ASCII table.

Please help me to solve this issue.

Thanks.

推荐答案

最好使用base64编码: ^ ]
It''s better to use base64 encoding : Convert.ToBase64String Method (Byte[])[^]


^ ]
字节189在iso-8859-1中表示"1/2"(又名"Latin-1"),因此以下可能是您想要的:

http://stackoverflow.com/questions/666385/how-can-i-convert-extended-ascii-to-a-system-string[^]
Byte 189 represents a "½" in iso-8859-1 (aka "Latin-1"), so the following is maybe what you want:

var e = Encoding.GetEncoding("iso-8859-1");
var s = e.GetString(new byte[] { 189 });


.NET中的所有字符串和字符都是UTF-16编码的,因此您需要使用编码器/解码器来转换其他任何内容,有时这是默认设置(例如,对于FileStream实例为UTF-8),但是好的做法是始终指定. />
您将需要某种形式的隐式或(更好)显式元数据,以向您提供有关哪种编码的信息.


All strings and chars in .NET are UTF-16 encoded, so you need to use an encoder/decoder to convert anything else, sometimes this is defaulted (e.g. UTF-8 for FileStream instances) but good practice is to always specify.

You will need some form of implicit or (better) explicit metadata to supply you with the information about which encoding.


这篇关于用C#将扩展的ASCII字符写入字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆