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

时间:2022-10-21 21:00:09

代码如下:

 public class Main_bytesToStr {

     public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
System.out.println("defaultCharset: " + Charset.defaultCharset().name());
System.out.println("file.encoding:" + System.getProperty("file.encoding"));
System.out.println(); String word = "a好";// 字符或字符串在Java内存中始终以内部编码即UTF-16保存。且采用大端
printTransStr(word, "ISO-8859-1");
printTransStr(word, "GBK");
printTransStr(word, "Unicode");
printTransStr(word, "UTF-16");
printTransStr(word, "UTF-16BE");
printTransStr(word, "UTF-16LE");
System.out.println(); InputStreamReader ir = new InputStreamReader(System.in); } public static void printTransStr(String word, String charset) throws UnsupportedEncodingException {
System.out.println("--------" + word + " " + charset + "--------");
byte[] bytes = word.getBytes(charset);
System.out.println(binaryToStr(bytes, 2));
System.out.println(binaryToStr(bytes, 8));
System.out.println(binaryToStr(bytes, 10));
System.out.println(binaryToStr(bytes, 16));
} /**
* 将byte[]转为各种进制的字符串
*
* @param bytes
* byte[]
* @param radix
* 基数可以转换进制的范围,从Character.MIN_RADIX到Character.MAX_RADIX,超出范围后变为10进制
* @return 转换后的字符串
*/
public static String binaryToStr(byte[] bytes, int radix) {
return new BigInteger(1, bytes).toString(radix);// 这里的1代表正数
// System.out.printf("%x ",bytes[0]);
}
}

结果如下:

 defaultCharset: GBK
file.encoding:GBK --------a好 ISO-8859-1--------
110000100111111
60477
24895
613f
--------a好 GBK--------
11000011011101011000011
30335303
6404803
61bac3
--------a好 Unicode--------
111111101111111100000000011000010101100101111101
7757740030254575
280371176495485
feff0061597d
--------a好 UTF-16--------
111111101111111100000000011000010101100101111101
7757740030254575
280371176495485
feff0061597d
--------a好 UTF-16BE--------
11000010101100101111101
30254575
6379901
61597d
--------a好 UTF-16LE--------
1100001000000000111110101011001
14100076531
1627422041
61007d59

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