1.Integer缺点
目前测试Integer只能一次性转4*7位二进制数,也就是7位16进制,故进行改进
2.改进
操作:每四位二进制一转换,以免到上限报错
注解格式:序号(代码顺序)+解释
public class Main {public static void main(String[] args) {//1.测试文本:创建int数组放入16进制为0123456789abcd的二进制int[] key={0,0,0,0,0,0,0,1,0,0,1,0,0,0,1,1,0,1,0,0,0,1,0,1,0,1,1,0,0,1,1,1,1,0,0,0,1,0,0,1,1,0,1,0,1,0,1,1,1,1,0,0,1,1,0,1,1,1,1,0,1,1,1,1};//2.s1连接存储int转Sting的值String s1=new String();//3.for循环将int转Sting存入s1,每4位2进制数转16进制并输出,因parseInt转换过长的会报错for(int i=0;i<key.length;i++){s1 +=(key[i]+"");if((i+1)%4==0&&i!=0){//4.substring用来获取每次新转换的四位数后利用Integer的方法转换int x = Integer.parseInt((s1).substring(i-3,i+1), 2);String shiliu = Integer.toHexString(x);System.out.print(shiliu);}}}
}