-3
String a="b5a99e49708ecf072f189b4f85007c76990ef305";
String b="a7d55b1a392a1f34ab95453817fdd49df140c486";

Example i converted string to decimal "1". it was 6 bits. how do i do it 7bits? and use or operator string a and b;

Here is my code

char[] c = a.toCharArray();
char[] d = b.toCharArray();

for (int i = 0; i < str.length(); i++)
{
    r += Integer.toBinaryString(c[i]|d[i]);     
}
Ousmane D.
  • 50,173
  • 8
  • 66
  • 103

2 Answers2

0

If I have understood correctly, you just need to prepend the correct number of zeroes to your binary string so there are 7 bits in all. The standard trick to generate a small fixed number of a character is to use String.substring() on a constant string of that character. Since a char fits in 16 bits, we will never need more than 16 zeroes:

    String binaryString = Integer.toBinaryString(ch);
    int missingZeroes = desiredNumberOfBits - binaryString.length();
    if (missingZeroes > 0) {
        binaryString = "0000000000000000".substring(0, missingZeroes) + binaryString;
    }
    System.out.println(binaryString);

If ch is a char holding a 1, the above prints

0110001

You can fill in c[i] | d[i] instead of ch if that is what you require.

You may want to add a defensive check that the string from Integer.toBinaryString() is not already too long.

Ole V.V.
  • 65,573
  • 11
  • 96
  • 117
-1

You can combine these hexadecimal values as Strings this way.

char[] c = a.toCharArray();
char[] d = b.toCharArray();
char[] r = new char[Math.min(a.length, b.length)];
String hex = "0123456789abcdef";

for (int i = 0; i < r.length; i++) {
    int ci = Character.getNumericValue(c[i]);
    int di = Character.getNumericValue(d[i]);
    int ri = ci | di;
    r[i] = hex.charAt(ri);
}
String s = new String(r);
Peter Lawrey
  • 498,481
  • 72
  • 700
  • 1,075