我如何转换ASCII扩展到以System.String? [英] How can I convert extended ascii to a System.String?

查看:86
本文介绍了我如何转换ASCII扩展到以System.String?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如:半或ASCII十二月189。当我读文本的字节文件的字节[]包含有效的值,在这种情况下,189

For example: "½" or ASCII DEC 189. When I read the bytes from a text file the byte[] contains the valid value, in this case 189.

转换为统一code导致的Uni code替换字符65533.

Converting to Unicode results in the Unicode replacement character 65533.

统一codeEncoding.Uni code.GetString(B);

UnicodeEncoding.Unicode.GetString(b);

转换为ASCII码结果63或?

Converting to ASCII results in 63 or "?"

ASCIIEncoding.ASCII.GetString(B);

ASCIIEncoding.ASCII.GetString(b);

如果这是不可能的是什么,以处理该数据的最佳方法?我希望能够执行字符串功能,如更换()。

If this isn't possible what is the best way to handle this data? I'd like to be able to perform string functions like Replace().

推荐答案

字节189重新presents在ISO-8859-1½(又名拉丁-1),所以下面也许是你想要的:

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连接codeD,所以你需要使用EN codeR /日codeR到别的转换,有时这也被拖欠(如UTF- 8的FileStream实例),但良好的做法是始终指定。

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.

这篇关于我如何转换ASCII扩展到以System.String?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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