Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

String to Byte

Posted on 2006-11-09
10
Medium Priority
?
193 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
10 Comments
 
LVL 92

Assisted Solution

by:objects
objects earned 400 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 200 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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 learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

718 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