Created
April 28, 2022 05:40
-
-
Save dblevins/63e74bde846558bbe8152d9818b3a45a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.ByteArrayOutputStream; | |
import java.io.IOException; | |
import java.io.OutputStreamWriter; | |
import java.nio.charset.StandardCharsets; | |
public class Foo { | |
public static void main(String[] args) throws IOException { | |
final char c = 'ñ'; | |
final ByteArrayOutputStream baos = new ByteArrayOutputStream(); | |
final OutputStreamWriter writer = new OutputStreamWriter(baos); | |
writer.write(c); | |
writer.close(); | |
final String string = baos.toString("UTF-8"); | |
System.out.printf("string: %s%n", string); | |
System.out.printf("length: %s%n", string.length()); | |
System.out.printf(" chars: %s%n", string.toCharArray().length); | |
System.out.printf(" bytes: %s%n", baos.toByteArray().length); | |
System.out.printf(" bytes: %s%n", string.getBytes(StandardCharsets.UTF_8).length); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
string: ñ | |
length: 1 | |
chars: 1 | |
bytes: 2 | |
bytes: 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment