简体   繁体   English

从字节中读取特殊字符[]

[英]Reading special characters from Byte[]

I'm writing and reading from Mifare - RFID cards. 我正在writing Mifare writingreading - RFID卡。

To WRITE into the card, i'm using a Byte[] like this: WRITE卡片,我正在使用像这样的Byte[]

byte[] buffer = Encoding.ASCII.GetBytes(txt_IDCard.Text);  

Then, to READ from the card, I'm getting some error with special characters , when it's supposed to show me é, ã, õ, á, à... I get ? 然后,从卡片中READ ,我收到一些special characters错误,当它应该显示我é, ã, õ, á, à...我明白了? instead: 代替:

string result = System.Text.Encoding.UTF8.GetString(buffer);
string result2 = System.Text.Encoding.ASCII.GetString(buffer, 0, buffer.Length);
string result3 = Encoding.UTF7.GetString(buffer); 

eg: Instead I get Àgua, amanhã, você I receive/read ?gua, amanh?, voc? 例如:相反,我得到了Àgua, amanhã, você我收到/读了?gua, amanh?, voc? .
How may I solve it ? 我该怎么解决?

ASCII by its very definition only supports 128 characters. ASCII的定义只支持128个字符。

What you need is ANSI characters if you are reading legacy text. 如果您正在阅读遗留文本,那么您需要的是ANSI字符。

You can use Encoding.Default instead of Encoding.ASCII to interpret characters in the current locale's default ANSI code page. 您可以使用Encoding.Default而不是Encoding.ASCII来解释当前语言环境的默认ANSI代码页中的字符。

Ideally, you would know exactly which code page you are expecting the ANSI characters to use and specify the code page explicitly using this overload of Encoding.GetEncoding(int codePage) , for example: 理想情况下,您可以确切地知道要使用ANSI字符的代码页,并使用Encoding.GetEncoding(int codePage)重载明确指定代码页,例如:

string result = System.Text.Encoding.GetEncoding(1252).GetString(buffer);

Here's a very good reference page on Unicode: http://www.joelonsoftware.com/articles/Unicode.html 这是关于Unicode的非常好的参考页面: http//www.joelonsoftware.com/articles/Unicode.html

And another here: http://msdn.microsoft.com/en-us/library/b05tb6tz%28v=vs.90%29.aspx 另一个在这里: http//msdn.microsoft.com/en-us/library/b05tb6tz%28v=vs.90%29.aspx

But maybe you can just use UTF8 when reading and writing 但也许你可以在阅读和写作时使用UTF8

I don't know the details of the card reader. 我不知道读卡器的细节。 Is the data you read and write to the card just a load of bytes? 您读取和写入卡的数据是否只是一个字节的负载?

If so, you can just use UTF8 for both reading and writing and it will all just work. 如果是这样,你可以使用UTF8进行读写,这一切都可以正常工作。 It's only necessary to use ANSI if you are working with a legacy device which is expecting (or providing) ANSI text. 如果您正在使用期望(或提供)ANSI文本的旧设备,则只需使用ANSI。 If the device just stores bytes blindly without implying any particular format, you can do what you like - in this case, just always use UTF8. 如果设备只是盲目地存储字节而不暗示任何特定格式,你可以做你喜欢的事情 - 在这种情况下,只需要总是使用UTF8。

It seems like you're using characters that aren't mapped in the 7 bits ASCII, but in the "extensions" ISO-8859-1 or ISO-8859-15. 您似乎使用的是未在7位ASCII中映射的字符,而是在“扩展”ISO-8859-1或ISO-8859-15中。 You'll need to choose a specific encoding for mapping to your byte array and things should work fine; 你需要选择一个特定的编码来映射到你的字节数组,事情应该可以正常工作;

byte[] buffer = Encoding.GetEncoding("ISO-8859-1").GetBytes(txt_IDCard.Text);

You have two problems there: 你有两个问题:

  1. ASCII supports only a limited amount of characters. ASCII仅支持有限数量的字符。
  2. You're currently using two different Encodings for reading and writing. 您目前正在使用两种不同的编码进行读写。

You should write with the same Encoding as you read. 您应该使用与阅读时相同的编码进行编写。

Writing 写作

    byte[] buffer = Encoding.UTF8.GetBytes(txt_IDCard.Text);  

Reading

    string result = Encoding.UTF8.GetString(buffer);

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

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