Search code examples
javavb6asciinon-ascii-characters

ASC Visual Basic for Java


I need a function on Java that do the same as ASC function on Visual Basic. I've had looking for it on internet, but I can't found the solution.

The String that I have to know the codes was created on Visual Basic. It's according to ISO 8859-1 and Microsoft Windows Latin-1 characters. The ASC function on Visual Basic knows those codes, but in Java, I can't find a function that does the same thing.

I know in Java this sentence:

String myString = "ÅÛ–ßÕÅÝ•ÞÃ";
int first = (int)string.chartAt(0); // "Å"- VB and Java returns: 197
int second = (int)string.chartAt(0); // "Û" - VB and Java returns: 219
int third = (int)string.chartAt(0); // "–" - VB returns: 150 and Java returns: 8211

The first two characters, I haven't had problem, but the third character is not a ASCII code.

How can I get same codes in VB and Java?


Solution

  • First of all, note that ISO 8859-1 != Windows Latin-1. (See http://en.wikipedia.org/wiki/Windows-1252)

    The problem is that Java encodes characters as UTF16, so casting to int will generally result in the Unicode value of the char.

    To get the Latin-1 encoding of a char, first convert it to a Latin-1 encoded byte array:

    public class Encoding {
    
        public static void main(String[] args) {
            // Cp1252 is Windows codepage 1252
            byte[] bytes = "ÅÛ–ßÕÅÝ•ÞÃ".getBytes(Charset.forName("Cp1252"));
            for (byte b: bytes) {
                System.out.println(b & 255);
            }
        }
    
    }
    

    prints:

    197
    219
    150
    223
    213
    197
    221
    149
    222
    195