1

So I am writing a program in Java that does amplitude demodulation. I am reading in a sine wave value by value and determining whether I have a '1' or a '0'. For now I am saving them as chars, but am open to suggestions. I know the number of samples per bit, so determining the bit is not an issue.

I can't figure out a way to convert every set of 8 bits in to a byte, so I can then decode the final array of bytes using UTF-8. Is there an effective way of doing that?

user3333414
  • 125
  • 1
  • 2
  • 7

2 Answers2

3

Try this, from the Byte Java API

String myBitString = "10101010";
int radixTwo = 2;
byte myByteValue = Byte.parseByte(myBitString, radixTwo)

Obviously you can add the byte values to a byte[] array and then use a loop afterwards to display / decode them.

J Richard Snape
  • 20,116
  • 5
  • 51
  • 79
0

what could be helpful is a conversion method...

public static byte toByte(boolean[] bits){
    assert bits.length == 8;
    int tmp = 0;
    for (int i = 0; i < 8; i ++){
        if(bits[0] ){
            tmp = tmp + (2^i);
        }
    }
    return (byte)(tmp)
}
Martin Frank
  • 3,445
  • 1
  • 27
  • 47