標籤:odi ring 編碼 rgs 多位元組 throw public log 記憶體
代碼如下:
1 public class Main_bytesToStr { 2 3 public static void main(String[] args) throws IOException { 4 // TODO Auto-generated method stub 5 System.out.println("defaultCharset: " + Charset.defaultCharset().name()); 6 System.out.println("file.encoding:" + System.getProperty("file.encoding")); 7 System.out.println(); 8 9 String word = "a好";// 字元或字串在Java記憶體中始終以內部編碼即UTF-16儲存。且採用大端10 printTransStr(word, "ISO-8859-1");11 printTransStr(word, "GBK");12 printTransStr(word, "Unicode");13 printTransStr(word, "UTF-16");14 printTransStr(word, "UTF-16BE");15 printTransStr(word, "UTF-16LE");16 System.out.println();17 18 InputStreamReader ir = new InputStreamReader(System.in);19 20 }21 22 public static void printTransStr(String word, String charset) throws UnsupportedEncodingException {23 System.out.println("--------" + word + " " + charset + "--------");24 byte[] bytes = word.getBytes(charset);25 System.out.println(binaryToStr(bytes, 2));26 System.out.println(binaryToStr(bytes, 8));27 System.out.println(binaryToStr(bytes, 10));28 System.out.println(binaryToStr(bytes, 16));29 }30 31 /**32 * 將byte[]轉為各種進位的字串33 * 34 * @param bytes35 * byte[]36 * @param radix37 * 基數可以轉換進位的範圍,從Character.MIN_RADIX到Character.MAX_RADIX,超出範圍後變為10進位38 * @return 轉換後的字串39 */40 public static String binaryToStr(byte[] bytes, int radix) {41 return new BigInteger(1, bytes).toString(radix);// 這裡的1代表正數42 // System.out.printf("%x ",bytes[0]);43 }44 }
結果如下:
1 defaultCharset: GBK 2 file.encoding:GBK 3 4 --------a好 ISO-8859-1-------- 5 110000100111111 6 60477 7 24895 8 613f 9 --------a好 GBK--------10 1100001101110101100001111 3033530312 640480313 61bac314 --------a好 Unicode--------15 11111110111111110000000001100001010110010111110116 775774003025457517 28037117649548518 feff0061597d19 --------a好 UTF-16--------20 11111110111111110000000001100001010110010111110121 775774003025457522 28037117649548523 feff0061597d24 --------a好 UTF-16BE--------25 1100001010110010111110126 3025457527 637990128 61597d29 --------a好 UTF-16LE--------30 110000100000000011111010101100131 1410007653132 162742204133 61007d59
從Unicode或UTF-16的結果也可以看出,JVM採用大端方式存多位元組的資料。
Java位元組數組轉按radix進位輸出