码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2016-11-10 02:41:46      阅读:233      评论:0      收藏:0      [点我收藏+]

标签: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 bytes
35      *            byte[]
36      * @param radix
37      *            基数可以转换进制的范围,从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 11000011011101011000011
11 30335303
12 6404803
13 61bac3
14 --------a好  Unicode--------
15 111111101111111100000000011000010101100101111101
16 7757740030254575
17 280371176495485
18 feff0061597d
19 --------a好  UTF-16--------
20 111111101111111100000000011000010101100101111101
21 7757740030254575
22 280371176495485
23 feff0061597d
24 --------a好  UTF-16BE--------
25 11000010101100101111101
26 30254575
27 6379901
28 61597d
29 --------a好  UTF-16LE--------
30 1100001000000000111110101011001
31 14100076531
32 1627422041
33 61007d59

 从Unicode或UTF-16的结果也可以看出,JVM采用大端方式存多字节的数据。

 

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

标签:odi   ring   编码   rgs   多字节   throw   public   log   内存   

原文地址:http://www.cnblogs.com/z-sm/p/6049125.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!