强制C#使用ASCII [英] Force C# to use ASCII

查看:138
本文介绍了强制C#使用ASCII的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作在C#应用程序,并需要阅读并从一个特定的数据文件的格式写的。目前唯一的问题是,该格式使用严格的单字节字符,和C#不停地尝试在Unicode中抛出,当我用一个作家和一个字符数组(双打文件大小,以及其他严重的问题)。我一直对修改使用字节数组,而不是代码,但喂它们成树视图和DataGrid控件时,导致一些投诉,涉及转换和诸如此类的东西。



我花了一点时间google搜索,并且似乎没有成为一个简单的typedef我可以用它来强制字符键入为使用字节我计划,至少在没有导致额外的并发症。



有没有一种简单的方法来强制C#的。NET 计划ASCII只使用,不碰的Unicode?

后来,我得到这个工作差不多。在BinaryReader /作家使用 ASCIIEncoding 结束了固定的大多数问题(的一些问题有一个多余的字符被追加到字符串发生,但我固定的了)。我有最后一个问题,这是非常小的,但可能是很大的:在该文件中,一个特定的字符(如打印欧元符号)被转换为 什么时候?我加载/保存的文件。这不是在文本的问题不大,但是,如果它发生在记录长度,它可以由千字节改变大小(不好,很明显)。 ?我认为这是由编码引起的,但如果从文件来了,为什么不能将其回去



精确的问题/结果是这样的:




  • 原始文件:0x80的(欧元)


  • 的编码:
    ** ASCII:0x3F的
    ** UTF8(?):0xC280(A-帽子欧元)




无论这些结果的将工作,因为文件中的任何位置,它可以改变(如果80改变到3F在记录长度整型,它可以是65 *(256 ^ 3)之差)。不好。我试图用一个 UTF-8编码,盘算这将解决这个问题相当好,但它现在补充说,第二个字符,这是雪上加霜。


解决方案

C#(.NET)将始终使用Unicode字符串。这是由设计



在读取或写入文件,你可以,但是,使用一个StreamReader / StreamWriter的设置为强制ASCII编码,就像这样:

  StreamReader的读者=新的StreamReader(FILESTREAM,新ASCIIEncoding()); 



然后,只需读取使用的StreamReader。



写作是一样的,只是使用的StreamWriter。


I'm working on an application in C# and need to read and write from a particular datafile format. The only issue at the moment is that the format uses strictly single byte characters, and C# keeps trying to throw in Unicode when I use a writer and a char array (which doubles filesize, among other serious issues). I've been working on modifying the code to use byte arrays instead, but that causes a few complaints when feeding them into a tree view and datagrid controls, and it involves conversions and whatnot.

I've spent a little time googling, and there doesn't seem to be a simple typedef I can use to force the char type to use byte for my program, at least not without causing extra complications.

Is there a simple way to force a C# .NET program to use ASCII-only and not touch Unicode?

Later, I got this almost working. Using the ASCIIEncoding on the BinaryReader/Writers ended up fixing most of the problems (a few issues with an extra character being prepended to strings occurred, but I fixed that up). I'm having one last issue, which is very small but could be big: In the file, a particular character (prints as the Euro sign) gets converted to a ? when I load/save the files. That's not an issue in texts much, but if it occurred in a record length, it could change the size by kilobytes (not good, obviously). I think it's caused by the encoding, but if it came from the file, why won't it go back?

The precise problem/results are such:

  • Original file: 0x80 (euro)

  • Encodings: ** ASCII: 0x3F (?) ** UTF8: 0xC280 (A-hat euro)

Neither of those results will work, since anywhere in the file, it can change (if an 80 changed to 3F in a record length int, it could be a difference of 65*(256^3)). Not good. I tried using a UTF-8 encoding, figuring that would fix the issue pretty well, but it's now adding that second character, which is even worse.

解决方案

C# (.NET) will always use Unicode for strings. This is by design.

When you read or write to your file, you can, however, use a StreamReader/StreamWriter set to force ASCII Encoding, like so:

StreamReader reader = new StreamReader (fileStream, new ASCIIEncoding());

Then just read using StreamReader.

Writing is the same, just use StreamWriter.

这篇关于强制C#使用ASCII的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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