Solved

String to Byte

Posted on 2006-11-09
10
188 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Weekend adv creator 3 52
netstat -ano | find "8000" and taskkill /f /pid 2984 3 83
javap bin 2 41
passing enum to a method 4 23
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
The viewer will learn how to implement Singleton Design Pattern in Java.

838 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