首页 > 代码库 > Java字节数组转按radix进制输出

Java字节数组转按radix进制输出

代码如下:

 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进制输出