简体   繁体   中英

Convert two-digit hex code to Unicode equivalent

I have a print format page that uses hex values to represent extended ASCII characters (resembling characters from IBM code page 437 ). I am using this table for reference. I have no say over the format page so this cannot be changed.

This print format page is used in my Android application to place relevant data in the right place when sent to the printer. The final result should look something like an invoice.

I am having trouble with the representation of certain ASCII characters in Java. For example, from the table above, characters 201, 205, and 187. When combined in that order they from a nice border for my data table.

In Java I am trying to convert from hex to ASCII like this:

String hex = "D5"; //this is just for example
int n = Integer.parseInt(hex, 16); //this should return a value of 213
char c = (char)n;

"c" ends up being some wacky looking character that is not what I intended. I realize now it's because Java is using Windows-1252/CP-1252 standard .

I think the solution would be to convert from the two-digit hex value to the corresponding Unicode value, but cannot figure out how. Any help would be appreciated.

Why do you want to go the extra mile?

private static final char TOP_LEFT = '╔';
private static final char HORIZONTAL = '═';
private static final char TOP_RIGHT = '╗';

And just use these... But beware, different fonts may represent these differently...

听起来您可以使用带byte []和Charset参数的String构造函数,并使用适合于您要接收的数据的字符集(“ IBM437”)。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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