Results 1 to 6 of 6
  1. #1
    telltera is offline Member
    Join Date
    Sep 2010
    Posts
    4
    Rep Power
    0

    Default converting to binary

    Hi all,

    I am new to Java although not new to programming :)
    I do not know how to convert an ASCII(some string value) to binary using some existing useful methods.

    So here is an eg.

    Java Code:
    byte[] someBytesToSign = "hello".getBytes();
    			System.out.print("orig bytes is"+Arrays.toString(someBytesToSign));
    			int i = Integer.parseInt(Arrays.toString(someBytesToSign));  
    			  String by = Integer.toBinaryString(i);
    			  System.out.print("Binary: " + by);
    With Arrays.toString, I get the decimal values of the ASCII but in an array of "[....]" format.

    And I had no success converting it into binary using the Integer.toBinaryString :( I know something is wrong with the data type conversion but dunno what.


    Well, my actual problem is this. I have a byte array and I want to see the actual bits in this array.

    If I do


    Java Code:
    byte[] someBytesToSign = "hello".getBytes();
    			System.out.print("orig bytes is"+someBytesToSign);
    I only see the bytes of the OBJECT and not the bytes of the value stored in the byte object.. Am I making sense??

    Please help.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Since you have to content in a byte array, get each binary of the array and find the relevant using integer value of it. Let me know if you are confusing what I'm saying.

  3. #3
    telltera is offline Member
    Join Date
    Sep 2010
    Posts
    4
    Rep Power
    0

    Default

    Yes, I am a little confused.

    I actually don't know how I can get the binary of each element of that array.

    Can you elaborate

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    I had no success converting it into binary using the Integer.toBinaryString
    Can you show the output and explain why it is not what you want?

    Can you comment each line of the code you've posted describing what that line is supposed to do?
    The code doesn't make any sense to me.
    Last edited by Norm; 09-01-2010 at 01:42 PM.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by telltera View Post
    Yes, I am a little confused.

    I actually don't know how I can get the binary of each element of that array.

    Can you elaborate
    In each byte you have 8 bits. So while shifting one bit at a time, compare it with the full range,

    Java Code:
    (your_bute & 128 ) == 0 ? 0 : 1;
    then shift it,
    Java Code:
    your_byte <<= 1;

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Replies: 2
    Last Post: 07-09-2010, 12:37 PM
  2. Converting mp3 files to binary
    By kylefrank in forum Advanced Java
    Replies: 4
    Last Post: 06-09-2010, 08:25 PM
  3. Replies: 3
    Last Post: 07-25-2009, 01:44 PM
  4. converting image to binary code n vice versa
    By nupurashi in forum Advanced Java
    Replies: 4
    Last Post: 04-01-2009, 07:27 PM
  5. Converting a text file int binary
    By sruthi_2009 in forum New To Java
    Replies: 0
    Last Post: 03-23-2009, 03:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •