Solved

String to Byte

Posted on 2006-11-09
10
185 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
 

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

706 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now