問題描述
我也想將我的二進(jìn)制(在字符串中)轉(zhuǎn)換為十六進(jìn)制字符串,這只是一個程序片段,因為這個程序只是另一個更大程序的一部分:
I want to convert my binary(which is in string) to hexadecimal string also, this is just a program fragment since this program is just a part of another bigger program:
//the variable name of the binary string is: "binary"
int digitNumber = 1;
int sum = 0;
int test = binary.length()%4;
if(test!=0) {
binary = padLeft(binary, test);
}
for(int i = 0; i < binary.length(); i++){
if(digitNumber == 1)
sum+=Integer.parseInt(binary.charAt(i) + "")*8;
else if(digitNumber == 2)
sum+=Integer.parseInt(binary.charAt(i) + "")*4;
else if(digitNumber == 3)
sum+=Integer.parseInt(binary.charAt(i) + "")*2;
else if(digitNumber == 4 || i < binary.length()+1){
sum+=Integer.parseInt(binary.charAt(i) + "")*1;
digitNumber = 0;
if(sum < 10)
System.out.print(sum);
else if(sum == 10)
System.out.print("A");
else if(sum == 11)
System.out.print("B");
else if(sum == 12)
System.out.print("C");
else if(sum == 13)
System.out.print("D");
else if(sum == 14)
System.out.print("E");
else if(sum == 15)
System.out.print("F");
sum=0;
}
digitNumber++;
}
public static String padLeft(String s, int n) {
return String.format("%0$"+n+"s", s);
}//i added this for padding
問題是我不知道填充是否有效,但我確信這個程序返回錯誤的二進(jìn)制字符串的十六進(jìn)制轉(zhuǎn)換我正在嘗試這樣做:
the problem is that i dont know if the padding works but i am sure that this program return a wrong hexadecimal conversion of the binary string I am trying to do this:
http://www.wikihow.com/Convert-Binary-to-Hexadecimal
PS:我需要實現(xiàn)它(不使用任何內(nèi)置函數(shù))
推薦答案
如果您不必自己實現(xiàn)該轉(zhuǎn)換,則可以使用現(xiàn)有代碼:
If you don't have to implement that conversion yourself, you can use existing code :
int decimal = Integer.parseInt(binaryStr,2);
String hexStr = Integer.toString(decimal,16);
如果你必須自己實現(xiàn),你的代碼有幾個問題:
If you must implement it yourself, there are several problems in your code :
- 循環(huán)應(yīng)該從 0 迭代到 binary.length()-1(假設(shè)字符串的第一個字符代表最高有效位).
- 您隱含地假設(shè)您的二進(jìn)制字符串對于某個整數(shù) x 有 4*x 個字符.如果這不是真的,那么您的算法就會中斷.你應(yīng)該用零填充你的字符串以獲得這樣長度的字符串.
sum
必須在您輸出每個十六進(jìn)制數(shù)字后重置為 0.System.out.print(digitNumber);
- 這里應(yīng)該打印sum
,而不是digitNumber
.
- The loop should iterate from 0 to binary.length()-1 (assuming the first character of the String represents the most significant bit).
- You implicitly assume that your binary String has 4*x charcters for some integer x. If that's not true, your algorithm breaks. You should left pad your String with zeroes to get a String of such length.
sum
must be reset to 0 after each hex digit you output.System.out.print(digitNumber);
- here you should printsum
, notdigitNumber
.
這是大部分固定代碼的外觀:
Here's how the mostly fixed code looks :
int digitNumber = 1;
int sum = 0;
String binary = "011110101010";
for(int i = 0; i < binary.length(); i++){
if(digitNumber == 1)
sum+=Integer.parseInt(binary.charAt(i) + "")*8;
else if(digitNumber == 2)
sum+=Integer.parseInt(binary.charAt(i) + "")*4;
else if(digitNumber == 3)
sum+=Integer.parseInt(binary.charAt(i) + "")*2;
else if(digitNumber == 4 || i < binary.length()+1){
sum+=Integer.parseInt(binary.charAt(i) + "")*1;
digitNumber = 0;
if(sum < 10)
System.out.print(sum);
else if(sum == 10)
System.out.print("A");
else if(sum == 11)
System.out.print("B");
else if(sum == 12)
System.out.print("C");
else if(sum == 13)
System.out.print("D");
else if(sum == 14)
System.out.print("E");
else if(sum == 15)
System.out.print("F");
sum=0;
}
digitNumber++;
}
輸出:
7AA
這僅在二進(jìn)制位數(shù)可被 4 整除時才有效,因此您必須添加左側(cè) 0
填充作為 preliminray 步驟.
This will work only if the number of binary digits is divisable by 4, so you must add left 0
padding as a preliminray step.
這篇關(guān)于將二進(jìn)制字符串轉(zhuǎn)換為十六進(jìn)制字符串 JAVA的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!