0
 String message= "10";
        byte[] bytes = message.getBytes();

        for (int n = 0; n < bytes.length; n++) {
            byte b = bytes[n];

            for (int i = 0; i < 8; i++) {//do something for each bit in my byte
            boolean bit = ((b >> (7 - i) & 1) == 1);
            }
        }

My problem here is that it takes 1 and 0 as their ASCII values, 49 and 48, instead of 1 and 0 as binary(00000001 and 00000000). How can I make my program treat each character from my string as a binary sequence of 8 bits?

Basicly, I want to treat each bit of my number as a byte. I do that like this byte b = bytes[n]; but the program treats it as the ASCII value.

I could assign the number to an int, but then, I can't assign the bits to a byte.

  • 2 is not binary so you'll never be able to take "12" and get "11" base 2 out without doing some math (or using some helper classes). – mttdbrd Mar 13 '14 at 16:27
  • They *are* binary sequences of 8 bits. The character code 1 is 00110001 and 2 is 00110010. See an [ASCII table](http://www.asciitable.com/index/asciifull.gif). – Hot Licks Mar 13 '14 at 16:28
  • I know, but I want 1 to be represented as 00000001 and not 00110001. –  Mar 13 '14 at 16:30
  • Then subtract out 00110000 (which is '0'). – Hot Licks Mar 13 '14 at 16:30
  • Problem is, it's of type byte[] and I don't know exactly how to substract from it. Also edited original message, since I did a slight mistake. –  Mar 13 '14 at 16:31
  • Look at this answer -- http://stackoverflow.com/questions/47981/how-do-you-set-clear-and-toggle-a-single-bit-in-c-c/47990#47990 -- and it'll tell you how to get individual bits out of a byte. See the answer by @christopher below for how to turn an string into an int. – mttdbrd Mar 13 '14 at 16:45
  • `(byte)(myByteArray[n] - '0')` (The `(byte)` cast is needed to convert the result of the subtract (which will always be `int`) back to a byte (assuming that's what you want). – Hot Licks Mar 13 '14 at 17:29

3 Answers3

0

It's a bit messy, but the first thing that comes to mind is to first, split your message up into char values, using the toCharArray() method. Next, use the Character.getNumericValue() method to return the int, and finally Integer.toBinaryString.

Example

String message = "123456";

for(char c : message.toCharArray())
{
    int numVal = Character.getNumericValue(c);
    String binaryString = Integer.toBinaryString(numVal);

    for(char bit : binaryString)
    {
         // Do something with your bits.
    }
}
christopher
  • 26,815
  • 5
  • 55
  • 89
  • 1
    That's a little verbose. You can just do int myInt = Integer.parseInt(message); then get the bits out as you describe. Once its an int though, you can just do xor each bit to get it out. – mttdbrd Mar 13 '14 at 16:26
  • It tells me for-each not applicable expression type. –  Mar 13 '14 at 16:29
  • If you know it's an ASCII digit, just subtract `'0'` from it. – Hot Licks Mar 13 '14 at 16:29
  • @mttdbrd The idea of debate is to produce the best answer. Post a better answer and I'll gladly upvote. :) – christopher Mar 13 '14 at 16:36
  • What if I give it as an int, instead of string? For instance int message=0x0001. How can I include that in my program, to take the '1' as 00000001 and use it in my for loop? –  Mar 13 '14 at 16:45
  • @user3383062 See the comment I gave above. That thread has an excellent answer that tells you how to get each bit out of an int. – mttdbrd Mar 13 '14 at 16:46
  • Problem is, I want to treat each bit of my int as a byte. So, if I have 10, my number will be 0000000100000000(and then I will use each of these bits in my program). I just don't know how to treat my number like that. –  Mar 13 '14 at 16:51
0
String msg = "1234";

for(int i=0 ; i<msg.length() ; i++ ){
    String bits = Integer.toBinaryString(Integer.parseInt(msg.substring(i, i+1)));
   for(int j=0;j<8-bits.length();j++)
      bits = "0"+bits;
}

Now bits is a string of length 8.

1 00000001 10 00000010 11 00000011 100 00000100

You can use getBytes() on the String

user1544460
  • 173
  • 7
  • 18
  • bits needs to be of type byte. I will be shifting its bits in my for loop. –  Mar 13 '14 at 16:59
0

Use Java's parseInt(String s, int radix):

String message= "10";
int myInt = Integer.parseInt(message, 2); //because we are parsing it as base 2

At that point you have the correct sequence of bits, and you can do your bit-shifting.

boolean[] bits = new boolean[message.length()];
System.out.println("Parsed bits: ");
for (int i = message.length()-1; i >=0  ; i--) {
    bits[i] = (myInt & (1 << i)) != 0;
    System.out.print(bits[i] ? "1":"0");
}

System.out.println();

You could make it bytes if you really want to, but booleans are a better representation of bits...

MirroredFate
  • 12,396
  • 14
  • 68
  • 100