I try to encode/decode text that was written/read by a Visual Basic application. My code works for most special characters, but not for all - e.g. the EURO sign (�). In visual basic, the ASC() function returns 128 as the code which is correct when I look at the ANSI table at http://www.torsten-horn.de/techdocs/ascii.htm
But my Java code returns -128 (and other different numbers for other special characters:
final CharsetEncoder encoder = Charset.forName(encoding).newEncoder();
final String s = new String(plainText.getBytes(encoding), encoding);
System.out.println("canEncode: "+encoder.canEncode(s));
final ByteBuffer bb = encoder.encode(CharBuffer.wrap(s));
final byte[] text = bb.array();
final StringBuilder sb = new StringBuilder();
sb.append(new String(new byte[] {text})); //charatcer
sb.append(" (" + (int) text[i] +") : "); //code
System.out.println(sb.toString());
The output is:
VB: � (128)
Java: � (-128)
BTW: using Unicode/AscW() is no option as it doesn't work with other Microsoft components (Database, GUI controls, ...).