public class Test {
public static void main(String[] args) {
String str = "010111010110";
int[] a = toInt(str);
for(int i = 0;i<3;i++){
System.out.println(a[i]);
}
}
public static int[] toInt(String str){
if(str.length()!=12){
System.out.println("传入参数有误");
return null;
}else{
int[] targets = new int[3];
String tem1 = str.substring(0, 4);
String tem2 = str.substring(4, 8);
String tem3 = str.substring(8, 12);
targets[0] = Integer.parseInt(tem1);
targets[1] = Integer.parseInt(tem2);
targets[2] = Integer.parseInt(tem3);
return targets;
}
}
这里我把str平均分成3份,用Integer.parseInt()函数进行转换,可是他是按有符号类型转换的,得到的结果也不是整数,结果如下
101
1101
110
想要变成这样的形式,该怎么做
5
13
6
javaå°äºè¿å¶è£ æ¢ææ´æ°ï¼å¯ä»¥ä½¿ç¨Integerç±»çæ¹æ³ï¼æ¥è¿è¡è½¬æ¢ï¼å¦ä¸ï¼
Integer.parseInt("80000000",2)ï¼ Integerå±äºintåºæ¬ç±»åçå
è£
ç±»ï¼å¯ä»¥å®ç°å个类åçäºç¸è½¬æ¢ï¼è¿å¯ä»¥è¾åºæstringç±»åï¼åªéè¦ä½¿ç¨toString()æ¹æ³ã
懒得写了,自己看吧,代码给你
public static void main(String args[]) {