bit operations in Java

S

Sanket Suryawanshi

Hi friends,
I am doing a simulation on Data link layer. for which I need to pass
bits on the channel which is a Integer array.
to pass data in 0's and 1's I need to convert the
variable(char,int,String) into binary string.

Are there any functions in Java to convert CharToBinary, IntToBinary
which returns String and converts the BinaryToInt and BinaryToChar
also

plz help
 
J

Joona I Palaste

Sanket Suryawanshi said:
Hi friends,
I am doing a simulation on Data link layer. for which I need to pass
bits on the channel which is a Integer array.
to pass data in 0's and 1's I need to convert the
variable(char,int,String) into binary string.
Are there any functions in Java to convert CharToBinary, IntToBinary
which returns String and converts the BinaryToInt and BinaryToChar
also

What does a "binary string" mean? Do you mean a String object holding
the binary representation of an integer value, for example 0 becomes
"0", 1 becomes "1", 2 becomes "10", 3 becomes "11" and so on?
 
O

Oscar Kind

Sanket Suryawanshi said:
bits on the channel which is a Integer array.
to pass data in 0's and 1's I need to convert the
variable(char,int,String) into binary string.

Are there any functions in Java to convert CharToBinary, IntToBinary
which returns String and converts the BinaryToInt and BinaryToChar
also

Remember that a binary string is a number with radix 2.

Next, determine what you want to convert: a number (not a String; a String
probably needs to be parsed into a number).

Then, note that any (integer) number can be represented as a long, and
look at the API for java.lang.Long:
String binary = Long.toString(number, 2);
long number = Long.parseLong(binary, 2);

(note: this works slightly different with java.math.BigInteger)

Now you only have to catch the esceptions and you're set.


Oscar
 
J

Jon A. Cruz

Oscar said:
Remember that a binary string is a number with radix 2.


*Unless*, that is, he's coming from the viewpoint of C/C++ programmers
who often shove things in the 'strings' that are actually just byte arrays.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top