bit operations in Java

Discussion in 'Java' started by Sanket Suryawanshi, Jan 23, 2004.

  1. 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
     
    Sanket Suryawanshi, Jan 23, 2004
    #1
    1. Advertising

  2. Sanket Suryawanshi <> scribbled the following:
    > 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?

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "No, Maggie, not Aztec, Olmec! Ol-mec!"
    - Lisa Simpson
     
    Joona I Palaste, Jan 23, 2004
    #2
    1. Advertising

  3. Sanket Suryawanshi

    Oscar Kind Guest

    Sanket Suryawanshi <> wrote:
    > 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

    --
    No trees were harmed in creating this message.
    However, a large number of electrons were terribly inconvenienced.
     
    Oscar Kind, Jan 23, 2004
    #3
  4. Sanket Suryawanshi

    Jon A. Cruz Guest

    Oscar Kind wrote:
    > Sanket Suryawanshi <> wrote:
    >
    >>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.



    *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.
     
    Jon A. Cruz, Jan 23, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jesus M. Salvo Jr.
    Replies:
    2
    Views:
    4,280
    robert
    Feb 11, 2006
  2. mikeSpindler

    struct.unpack() and bit operations

    mikeSpindler, Sep 23, 2004, in forum: Python
    Replies:
    2
    Views:
    581
    Peter Otten
    Sep 23, 2004
  3. Replies:
    3
    Views:
    1,798
    Timothy Bendfelt
    Jan 19, 2007
  4. Replies:
    9
    Views:
    1,006
    Juha Nieminen
    Aug 22, 2007
  5. Jeff.M
    Replies:
    6
    Views:
    186
    Lasse Reichstein Nielsen
    May 4, 2009
Loading...

Share This Page