Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to encode Text in ANSI?

416044Jun 19 2007 — edited Jun 19 2007
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, ...).
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 17 2007
Added on Jun 19 2007
20 comments
1,995 views