Solved

String to Byte

Posted on 2006-11-09
10
189 Views
Last Modified: 2010-08-05
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?

Thanks in advance
0
Comment
Question by:akzah
  • 5
  • 3
  • 2
10 Comments
 
LVL 92

Assisted Solution

by:objects
objects earned 100 total points
ID: 17909942
0
 

Author Comment

by:akzah
ID: 17910025
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
 
LVL 92

Expert Comment

by:objects
ID: 17910157
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:akzah
ID: 17910201
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
 
LVL 92

Expert Comment

by:objects
ID: 17910235
int bits = Integer.parseInt(s, 2);
0
 

Author Comment

by:akzah
ID: 17910261
ah brilliant thanks very much, last point on how about when after the operations to get the string value back?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 50 total points
ID: 17910370
System.out.println(new BigInteger("10100100" , 2).shiftRight(2).toString(2));
0
 

Author Comment

by:akzah
ID: 17910558
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 Comment

by:akzah
ID: 17910563
sorry thats leftshift by 1 or rotation which i was doing but it does not make sense.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17912507
:-)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
difference of if loops 23 68
tomcat startup error 5 132
Android development question 2 75
Running JavaFX on the Raspberry Pi 27 120
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question