Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

windows-1252 to UTF-8

Below is the code I am trying to use, and the output it's giving me is:

RetValue: á, é, í, ó, ú, ü, ñ, ¿ Value: á, é, í, ó, ú, ü, ñ, ¿ ConvertValue: ?, ?, ?, ?, ?, ?, ?, ?

which is not the desired output. I think the output should be something of this kind %C3% for every character here.

public static void main(String[] args) {
    String value = "á, é, í, ó, ú, ü, ñ, ¿";
    String retValue = "";
    String convertValue = "";
    try {
        retValue = new String(value.getBytes(),
        Charset.forName("Windows-1252"));
        convertValue = new String(retValue.getBytes("Windows-1252"),
        Charset.forName("UTF-8"));
    } catch (Exception e) {
        e.printStackTrace();
    }
    System.out.println("RetValue: " + retValue + " Value: " + value
         + " ConvertValue: " + convertValue);
}
like image 878
Sharique Avatar asked Dec 25 '22 22:12

Sharique


1 Answers

I understand that you are trying to encode your text from default encoding to Windows-1252, then to UTF-8.

According to the javadoc for the String class

String(byte[] bytes, Charset charset)

Constructs a new String by decoding the specified array of bytes using the specified charset.

Therefore what you did was to decode a default encoded text into Windows-1252 and then further decode the newly obtained text into UTF-8. That's why it renders something abnormal.

If your purpose is to encode from Windows-1252 to UTF-8, I would suggest that you use the following approach with CharsetEncoder in java.nio package:

public static void main(String[] args) {
    String value = "á, é, í, ó, ú, ü, ñ, ¿";
    String retValue = "";
    String convertValue2 = "";
    ByteBuffer convertedBytes = null;
    try {
        CharsetEncoder encoder2 = Charset.forName("Windows-1252").newEncoder();
        CharsetEncoder encoder3 = Charset.forName("UTF-8").newEncoder();             
        System.out.println("value = " + value);

        assert encoder2.canEncode(value);
        assert encoder3.canEncode(value);

        ByteBuffer conv1Bytes = encoder2.encode(CharBuffer.wrap(value.toCharArray()));

        retValue = new String(conv1Bytes.array(), Charset.forName("Windows-1252"));

        System.out.println("retValue = " + retValue);

        convertedBytes = encoder3.encode(CharBuffer.wrap(retValue.toCharArray()));
        convertValue2 = new String(convertedBytes.array(), Charset.forName("UTF-8"));
        System.out.println("convertedValue =" + convertValue2);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

I obtained the following output:

value = á, é, í, ó, ú, ü, ñ, ¿

retValue = á, é, í, ó, ú, ü, ñ, ¿

convertedValue =á, é, í, ó, ú, ü, ñ, ¿

like image 134
alainlompo Avatar answered Dec 28 '22 08:12

alainlompo