Convert binary string to JAVA hex string

I also want to convert my binary (which is on the line) to a hexadecimal string, this is just a piece of the program, since this program is part of another larger 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 

the problem is that I don’t know if the add-on works, but I am sure that this program will return the wrong binary string hexadecimal conversion that I am trying to do:

http://www.wikihow.com/Convert-Binary-to-Hexadecimal

PS: I need to implement it (without using the built-in function)

+9
source share
7 answers

If you do not need to implement this conversion yourself, you can use the existing code:

 int decimal = Integer.parseInt(binaryStr,2); String hexStr = Integer.toString(decimal,16); 

If you must implement it yourself, there are several problems in your code:

  • The loop should start from 0 on binary.length () - 1 (assuming the first character of the string is the most significant bit).
  • You implicitly assume your binary string has 4 * x charcters for some integer x. If this is not the case, your algorithm is aborted. You must leave your String zeros to get a string of this length.
  • sum must be reset to 0 after each hexadecimal digit displayed.
  • System.out.print(digitNumber); - here you should type sum , not digitNumber .

Here's what basically fixed code looks like:

  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++; } 

Output:

7AA

This will only work if the number of binary bits is divisible by 4, so you should add padding 0 as a preliminary step.

+26
source

You can try something like this.

 private void bitsToHexConversion(String bitStream){ int byteLength = 4; int bitStartPos = 0, bitPos = 0; String hexString = ""; int sum = 0; // pad '0' to make input bit stream multiple of 4 if(bitStream.length()%4 !=0){ int tempCnt = 0; int tempBit = bitStream.length() % 4; while(tempCnt < (byteLength - tempBit)){ bitStream = "0" + bitStream; tempCnt++; } } // Group 4 bits, and find Hex equivalent while(bitStartPos < bitStream.length()){ while(bitPos < byteLength){ sum = (int) (sum + Integer.parseInt("" + bitStream.charAt(bitStream.length()- bitStartPos -1)) * Math.pow(2, bitPos)) ; bitPos++; bitStartPos++; } if(sum < 10) hexString = Integer.toString(sum) + hexString; else hexString = (char) (sum + 55) + hexString; bitPos = 0; sum = 0; } System.out.println("Hex String > "+ hexString); } 

Hope this helps: D

+3
source
  import java.util.*; public class BinaryToHexadecimal { public static void main() { Scanner sc=new Scanner(System.in); System.out.println("enter the binary number"); double s=sc.nextDouble(); int c=0; long s1=0; String z=""; while(s>0) { s1=s1+(long)(Math.pow(2,c)*(long)(s%10)); s=(long)s/10; c++; } while(s1>0) { long j=s1%16; if(j==10) { z="A"+z; } else if(j==11) { z="B"+z; } else if(j==12) { z="C"+z; } else if(j==13) { z="D"+z; } else if(j==14) { z="E"+z; } else if(j==15) { z="F"+z; } else { z=j+z; } s1=s1/16; } System.out.println("The respective Hexadecimal number is : "+z); } } 
0
source

Using the given binary number 01011011 we first convert it to a decimal number, each number will be Math.pow() along the decrement length:

(1 Γ— 2 (4)) + (1 Γ— 2 (3)) + (0 Γ— 2 (2)) + (1 Γ— 2 (1)) + (1 Γ— 2 (0))

= (0 Γ— 128) + (1 Γ— 64) + (0 Γ— 32) + (1 Γ— 16) + (1 Γ— 8) + (0 Γ— 4) + (1 Γ— 2) + (1 Γ— 1)

= 0 + 64 + 0 + 16 + 8 + 0 + 2 + 1

= 91 (decimal form of binary number)

Now after receiving the decimal number, we must convert it to a hexadecimal number.

So 91 is over 16. So we have to divide by 16.

After dividing by 16, the coefficient is 5, and the remainder is 11.

The remainder is less than 16.

The hexadecimal number of the remainder is B.

The coefficient is 5, and the hexadecimal number of the remainder is B.

That is, 91 = 16 Γ— 5 +11 = B

5 = 16 Γ— 0 + 5 = 5

= 5B

Implementation:

 String hexValue = binaryToHex(binaryValue); //Display result System.out.println(hexValue); private static String binaryToHex(String binary) { int decimalValue = 0; int length = binary.length() - 1; for (int i = 0; i < binary.length(); i++) { decimalValue += Integer.parseInt(binary.charAt(i) + "") * Math.pow(2, length); length--; } return decimalToHex(decimalValue); } private static String decimalToHex(int decimal){ String hex = ""; while (decimal != 0){ int hexValue = decimal % 16; hex = toHexChar(hexValue) + hex; decimal = decimal / 16; } return hex; } private static char toHexChar(int hexValue) { if (hexValue <= 9 && hexValue >= 0) return (char)(hexValue + '0'); else return (char)(hexValue - 10 + 'A'); } 
0
source

/ * * To change this license header, select the license headers in the project properties. * To modify this template file, select Tools | Templates * and open the template in the editor. * / batch line processing;

/ ** * * @author Zayeed Chowdhury * / public class StringProcessing {

 /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here int index = 0; String bin = ""; String[] hexString = new String[bin.length() / 4]; for (int i = 0; i < bin.length() / 4; i++) { hexString[i] = ""; for (int j = index; j < index + 4; j++) { hexString[i] += bin.charAt(j); } index += 4; } for (int i = 0; i < bin.length() / 4; i++) { System.out.print(hexString[i] + " "); } System.out.println("\n" + bin.length()); String[] result = binaryToHex(hexString); for (int i = 0; i < result.length; i++) { System.out.print("" + result[i].toUpperCase()); } System.out.println(""); } public static String[] binaryToHex(String[] bin) { String[] result = new String[bin.length]; for (int i = 0; i < bin.length; i++) { result[i] = Integer.toHexString(Integer.parseInt(bin[i], 2)); } //return Integer.toHexString(Integer.parseInt(bin[0], 2)); return result; } 

}

0
source

Use this for any length of a binary string:

 String hexString = new BigInteger(binaryString, 2).toString(16); 
0
source

private final String [] hexValues ​​= {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A "," B "," C "," D "," E "," F "};

 public void binaryToHexadecimal(String binary){ String hexadecimal; binary = leftPad(binary); System.out.println(convertBinaryToHexadecimal(binary)); } public String convertBinaryToHexadecimal(String binary){ String hexadecimal = ""; int sum = 0; int exp = 0; for (int i=0; i<binary.length(); i++){ exp = 3 - i%4; if((i%4)==3){ sum = sum + Integer.parseInt(binary.charAt(i)+"")*(int)(Math.pow(2,exp)); hexadecimal = hexadecimal + hexValues[sum]; sum = 0; } else { sum = sum + Integer.parseInt(binary.charAt(i)+"")*(int)(Math.pow(2,exp)); } } return hexadecimal; } public String leftPad(String binary){ int paddingCount = 0; if ((binary.length()%4)>0) paddingCount = 4-binary.length()%4; while(paddingCount>0) { binary = "0" + binary; paddingCount--; } return binary; } 
0
source

Source: https://habr.com/ru/post/974612/


All Articles