String to Byte

Hi

I am trying to create a program which takes in a string such as "10100100" and then preform bitwise operators on it such as right and left shift and output a string in the same format as the input.

I can only think of creating a method which will convert the string into a int manually and then create a byte with that int which I can use the operators on it. Anyway round that?

Who is Participating?

Commented:
System.out.println(new BigInteger("10100100" , 2).shiftRight(2).toString(2));
0

Commented:
0

Author Commented:
still need to work out the value of the string each time to use that so that does not help in making it easier. Thanks anyway tho.
0

Commented:
why? you just convert the string to a BitSet and do whatever you need. no need to worry about the string.
o/wise you're simply going to have to implement all the operations yourself.
0

Author Commented:
Not sure how I am gonna convert the string "10100100"  into a bitset when it needs a int to start it.

I don't want to go through the string working out what string represents. I don't know maybe I am confused. Otherwise I can use the byte b = 13; which is the same.
0

Commented:
int bits = Integer.parseInt(s, 2);
0

Author Commented:
ah brilliant thanks very much, last point on how about when after the operations to get the string value back?
0

Author Commented:
Thanks CEHJ however doing a left shift by two gives a weird answer:

System.out.println(new BigInteger("01100" , 2).shiftLeft(1).toString(2)); = 11000 which is correct however
System.out.println(new BigInteger("10000" , 2).shiftLeft(1).toString(2)); = 100000 which is wrong it should be 00001
0

Author Commented:
sorry thats leftshift by 1 or rotation which i was doing but it does not make sense.
0

Commented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.