2

Im currently working on a QRCode scanne and have come to a point where I've been stuck at for a while.

What I have so far is a String of 1s and 0s such as "100010100101....". What I wanted to do next ist turn this String into Bytes by always seperating 8 Bits.

With these Bytes I now want to decode them into text with this "ISO8859_1" Standart.

My Problem is the following: my results are way of what I want. This is my code:

for(int i = 0; i <= numberOfInt; i++){
        String character = "";
        for(int j = 0;j < 8; j++){
            boolean bool = tResult.remove(0); //tResult is a List of 1s & 0s
            if(bool){
                character = character + '1';
            }else{
                character = character + '0';
            }

        }       

        allcharacter[byteCounter] = (byte)Integer.parseInt(character,2);//I think this Line is where the mistake is. 
        byteCounter++; //Variable that counts where to put the next bit



    }
    String endresult ="";
    try {
        endresult = new String(allcharacter,"ISO8859_1");
    } catch (UnsupportedEncodingException e) {

        e.printStackTrace();
    } 
    return endresult;

What I think is that, the cast to (byte) doesn't work the way I understand it and therefore different bytes are saved into the array.

Thanks for any help.

antbre
  • 63
  • 6
  • The mistake is probably somewhere else. Apart from being far from optimal I don't see anything wrong in the code provided that tResult is a List of Booleans in groups of 8 ordered with most significant bit first - and each 8-Boolean-group represents an ISO 8859-1 character. We need at least sample input and the expected output for that input. – Markus Benko Mar 11 '17 at 20:50

2 Answers2

0

You can use the substring method of the String class to get the first 8 characters and then convert those 8 characters (treat them as bits) to a character (which is also 8 bits). Instead of parsing each character as an integer, and then casting it to a byte, you should check each character and multiply a byte value by 2 for each time you hit a 1. That way, you will get a value between 0-255 for each byte, which should give you a valid character.

Also you might want to check the Byte class and its methods, it probably has a method that already does this.

Edit: There you go.

Edit 2: Also this question may answer why your int to byte casting does not give you the result you thought it would.

Community
  • 1
  • 1
halileohalilei
  • 2,220
  • 2
  • 25
  • 52
0

Okay, I rarely work with bytes, so in that aspect I am useless. However, I have converted binary to string many times. The logic behind it is converting the binary string to a decimal int, then from int to char, then from char to string. Here is how I do that.

String list = "100111000110000111010011" //24 random binary digits for example
String output = "";
char letter = '';
int ascii = 0;
//Repeat while there is still something to read.
for(int i = 0; i < list.length(); i+=8){
    String temp = list.substring(i,i+8); //1 character in binary.
    for(int j = temp.length()-1; j >= 0; j--) //Convert binary to decimal
        if(temp.charAt(j) == '1')
            ascii += (int)Math.pow(2,j);
    letter = (char)ascii; //Sets the char letter to it's corresponding ascii value
    output = output + Character.toString(letter); //Adds the letter to the string
    ascii = 0; //resets ascii
}
System.out.println(output); //outputs the converted string

I hope that you found this helpful!

Josh Heaps
  • 335
  • 1
  • 8