Get string data from encoded bytes
I have an array of bytes that has come from a barcode reader (connected via a COM po开发者_JAVA技巧rt) reading an ID Card. When I convert these, I can read some of the data, for example:
Name, Surname, City
etc, but if some of the data has some characters like 'Ë' or 'Ç', or some characters that are used in our language [ed: OP is in Pristina, Kosovo], I get '?'. How can I get these characters through decoding?
You need to know the appropriate Encoding
that the device uses; it could be UTF-16, for example, in which case
string s = Encoding.Unicode.GetString(bytes);
or UTF-8:
string s = Encoding.UTF8.GetString(bytes);
but for regional encodings / code-pages you'll have to use:
string s = Encoding.GetEncoding(yourEncoding).GetString(bytes);
精彩评论