/** * Some experiments from CS257 class. Monday, 22 Sep 2014. */ public class Chars { public static void main(String[] args) { String message; String word = args[0]; message = String.format("Length of the command-line argument: %d", word.length()); System.out.println(message); message = String.format("Integer value of word[0] in base ten: %d", (int)word.charAt(0)); System.out.println(message); message = String.format("Integer value of word[0] in hexadecimal: 0x%04x", (int)word.charAt(0)); System.out.println(message); try { System.out.println("\n=== Bytes in command-line argument, considered as UTF-8 ==="); byte[] bytes = word.getBytes("UTF-8"); for (byte b : bytes) { message = String.format("0x%02x", (int)b); // message = String.format("0x%02x", ((int)b) & 0x000000ff); System.out.println(message); } } catch (Exception e) { System.out.println(e); } } }