Skip to content

Instantly share code, notes, and snippets.

@matvore
Last active August 20, 2018 17:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matvore/e981ee8e3ed1057d6a2aef45acf3189f to your computer and use it in GitHub Desktop.
Save matvore/e981ee8e3ed1057d6a2aef45acf3189f to your computer and use it in GitHub Desktop.
Demonstrate encoding with ASCII and UTF-8
import java.io.*;
public final class foo {
public static void main(String[] args) throws Exception {
byte b[] = {(byte) 0x80, (byte) 0xe3, (byte) 0x81, (byte) 0xbb};
ByteArrayInputStream is = new ByteArrayInputStream(b);
Reader r = new InputStreamReader(is, "ASCII");
System.out.println("ASCII:");
while (true) {
int c = r.read();
if (c == -1) break;
System.out.println(" " + c);
}
is = new ByteArrayInputStream(b);
r = new InputStreamReader(is, "UTF-8");
System.out.println("utf-8:");
while (true) {
int c = r.read();
if (c == -1) break;
System.out.println(" " + c);
}
}
}
/* Output:
ASCII:
65533
65533
65533
65533
utf-8:
65533
12411
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment