Search code examples
javaandroidvb6strconv

Is there a Java (Android) equivalent to VB6 Strconv


I have the following in an old VB6 class that I need to move to a Java class in Android.

tmp = StrConv(vValue, vbUnicode, AESLOCALE)

tmp = StrConv(vData, vbFromUnicode, AESLOCALE) 

where AESLOCALE is 1033

I have had a hunt around but cannot seem to work out how to tackle this. Thanks


Solution

  • It looks like you just need to convert back and forth between English (Locale 1033 or ISO_8859_1) and unicode (UTF_16).

    First, make sure you import the charsets:

        import static java.nio.charset.StandardCharsets.*;
    

    For the top line in your question you can use this to encode a charset in UTF-16:

        //Convert to unicode/UTF_16:
        byte[] engilshBytes = myString.getBytes(ISO_8859_1); 
        String unicodeValue = new String(engilshBytes, UTF_16); 
    

    For the bottom line in your question you can use this to encode unicode in ISO_8859_1:

        //Convert to english/ISO_8859_1:
        byte[] unicodeBytes = myString.getBytes(UTF_16); 
        String englishValue = new String(unicodeBytes, ISO_8859_1); 
    

    Edit:

    Link to the Android page on character sets (Works since Android 4.4):

    https://developer.android.com/reference/java/nio/charset/StandardCharsets

    Link to the Java page on character sets (NIO works since Java 7):

    https://docs.oracle.com/javase/8/docs/api/java/nio/charset/Charset.html