Encode to single byte extended ascii values

2019-02-17 11:59发布

问题:

In C# is there a way to encode the extended ascii values (128-255) into their single byte values as shown here: http://asciitable.com/

I've tried using Encoding.UTF8.GetBytes() but that returns multi byte values for the extended codes. I don't need anything beyond 255, but it would be nice to at least support those. I'm trying to send the text data to an Arduino running and LED matrix and want to handle accented letters, without having to deal with multibyte characters.

EDIT: To clarify, the LED matrix has no specific codepage. It's basically whatever I say it is. There's no built in text support in it or the arduino. It's just a dumb 128x8 pixel display and the controller is manually drawing the text pixel by pixel. Therefore, I'm actually providing a font (as a byte array in a header file) to it and can make any character code correspond to any output that I want... so, which codepage to use is not really an issue other than which one will give me full 8-bit characters.

回答1:

Just pass the code page number to the Encoding constructor. If what you linked is the correct "extended ASCII" table, that would be 437.

But IBM437 encoding is uncommon outside of DOS programs and Windows console apps. Otherwise, the standard encoding for Western European languages is ISO-8859-1 (Windows code page 28591) or windows-1252.



回答2:

You need to know the code page that the LED matrix uses. It is bound to be a standard one like 1252, the Windows code page for Western Europe and the Americas.

        var bytes = Encoding.GetEncoding(1252).GetBytes("Åãrdvárk");


回答3:

The Default encoding should handle that. Or use the ANSI codepage/encoding.