I have a piece of code in Java which prints out a String:
String original = new String("A" + "\u00ea" + "\u00f1" + "\u00fc" + "C");
byte[] utf8Bytes = original.getBytes("UTF8");
String roundTrip = new String(utf8Bytes, "UTF8");
System.out.println("roundTrip = " + roundTrip); // Output is [roundTrip = AêñüC]
When I run this piece of Java code in Scala, I get back AΩ±ⁿC
If I print the String in hex, outputs from Scala and Java match each other.
try {
String original = new String("A" + "\u00ea" + "\u00f1" + "\u00fc" + "C");
byte[] utf8Bytes = original.getBytes("UTF8");
byte[] defaultBytes = original.getBytes();
String roundTrip = new String(utf8Bytes, "UTF8");
printBytes(utf8Bytes, "utf8Bytes");
System.out.println();
printBytes(defaultBytes, "defaultBytes");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
Here's the output:
utf8Bytes[0] = 0x41
utf8Bytes[1] = 0xc3
utf8Bytes[2] = 0xaa
utf8Bytes[3] = 0xc3
utf8Bytes[4] = 0xb1
utf8Bytes[5] = 0xc3
utf8Bytes[6] = 0xbc
utf8Bytes[7] = 0x43
defaultBytes[0] = 0x41
defaultBytes[1] = 0xc3
defaultBytes[2] = 0xaa
defaultBytes[3] = 0xc3
defaultBytes[4] = 0xb1
defaultBytes[5] = 0xc3
defaultBytes[6] = 0xbc
defaultBytes[7] = 0x43
Reference: https://docs.oracle.com/javase/tutorial/i18n/text/string.html
Edit: I'm using the Scala to call the Java code.
How come the Scala out put is different from Java? How could I fix the Scala so that it will give me the same output as Java's?