## 关于2进制直接转16进制 原

sitaluoduoxi

/**
* 10 0010 1111 按每4个一组分开，在binarys[]中找到对应的下标idx，对应的hexs[idx]相连接。
* @param binaryStr 10 0010 1111
* @return  hexStr 22F
*/
public static String binaryToHex(String binaryStr){
String binarys[] = new String[]{
"0000","0001","0010","0011",
"0100","0101","0110","0111",
"1000","1001","1010","1011",
"1100","1101","1110","1111"
};
String hexs[] = new String[]{
"0","1","2","3",
"4","5","6","7",
"8","9","A","B",
"C","D","E","F"
};
int n = binaryStr.length()%4;
if(n != 0){
int zeroNum = 0;
zeroNum = 4 - n;
for(int i=0; i< zeroNum; i++){
binaryStr = "0"+binaryStr;
}
n = n+1;
}
String hexStr = "";
for(int j=0; j<binaryStr.length()/4; j++){
String temp = binaryStr.substring(j*4, (j+1)*4);
int index = -1;
for(int k=0;k<16;k++){
if(binarys[k].equals(temp)){
index = k;
}
}
if(index != -1){
hexStr = hexStr + hexs[index];
}
}

return hexStr;
}

/**
* 10 0010 1111 按每4个一组分开，转化为int型数据index，然后hexs[index]相加。
* @param binaryStr 10 0010 1111
* @return hexStr 22F
*/
public static String binaryToHex2(String binaryStr){

String hexs[] = new String[]{
"0","1","2","3",
"4","5","6","7",
"8","9","A","B",
"C","D","E","F"
};
int n = binaryStr.length()%4;
if(n != 0){
int zeroNum = 0;
zeroNum = 4 - n;
for(int i=0; i< zeroNum; i++){
binaryStr = "0"+binaryStr;
}
n = n+1;
}
String hexStr = "";
for(int j=0; j<binaryStr.length()/4; j++){
String temp = binaryStr.substring(j*4, (j+1)*4);

int index =0;//下标 index=15,则对应输出 hexs[15] = F
int i = 3;
for(char c : temp.toCharArray()) {
String str = new String(new char[]{c});
index = index + ((Integer.parseInt(str))<<i) ;
i--;
}
hexStr = hexStr + hexs[index];
}

return hexStr;

}

### sitaluoduoxi

#### 暂无文章

android 事件分发

7分钟前
0
0

12分钟前
0
0

public class Solution { private ArrayList<ArrayList<Integer>> listAll = new ArrayList<ArrayList<Integer>>(); ArrayList<ArrayList<Integer>> all= new ArrayList<ArrayList<Integer>>......

14分钟前
0
0

polly
15分钟前
0
0

22分钟前
1
0