Results 1 to 17 of 17
  1. #1
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default String to binary

    Hey there,
    I couldn't find any helpfull thread about it so I opened this one.
    How can I convery a string into a binary string?
    Thanks in advanced.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: String to binary

    What's a binary string?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Re: String to binary

    String st = "1100100"; // equals to 100 in DEC base
    It is a string, but the meaning of the string value is a vinary number.
    It's also can be saved as an int or long, I don't really care.
    The question is, how or is it even possible to convert a string into a binary number?

    I read a file into a string and I want to convert it to a binary, if you know another way to read it immediately into a binary string/int/long that would be even more efficient.
    Thanks.

  4. #4
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: String to binary

    Look for the java.lang.Integer's class parseInt() method in the Javadoc, I think that's what you are looking for.
    Last edited by wsaryada; 04-03-2012 at 08:26 AM.

  5. #5
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Re: String to binary

    parseInt get as a parameter a string and return an int, only for numbers.
    That's not what I'm looking for but thanks.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: String to binary

    You want something that converts a String representation of a number to an int.
    parseInt does that.
    What about parseInt do you think won't work with the String "1100100"?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Re: String to binary

    You want something that converts a String representation of a number to an int.
    I think you got me wrong,
    I want something that converts a string (I don't care what in there) and converts it to a binary if it possible.

    As far as I know in the end everything is translated into binary.. so I gusss it is possible.
    Example:
    String = "asdfok234234" and I want to get a binary string that will represent this string, so if I reverse it I will get my string back.

  8. #8
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: String to binary

    I think what you are trying to say is something called Base64 Encode / Decode. You can try to following example provided here: Learn Java by Examples - How do I encode binary data as Base64 string?.

    Here is the snippet:

    Java Code:
    String hello = "Hello World";
    byte[] encoded = org.apache.commons.codec.binary.Base64.encodeBase64(hello.getBytes());
    String encodedString = new String(encoded);
    The snippet above will change the "Hello World" into something like "SGVsbG8gV29ybGQ=". Then you can decode the string back to get the original value using the decodeBase64() method of this class. Base64 class is part of Apache Commons library.
    Last edited by wsaryada; 04-04-2012 at 06:39 AM.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: String to binary

    Quote Originally Posted by tnrh1 View Post
    I think you got me wrong,
    I want something that converts a string (I don't care what in there) and converts it to a binary if it possible.

    As far as I know in the end everything is translated into binary.. so I gusss it is possible.
    Example:
    String = "asdfok234234" and I want to get a binary string that will represent this string, so if I reverse it I will get my string back.
    What do you intend on doing with this binary representation?
    As that will determine how it gets converted.
    If you just want to take that String and show each character in binary on a separate line then loop over the char[] and display each one using Integer.toString() with a radix of 2.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default Re: String to binary

    Is this what you want to do: Convert each character in a String to a String of 8 binary digits.
    For example: A String of 3 characters would be converted to a String of 24 characters of (0 and 1s).
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Re: String to binary

    Quote Originally Posted by Norm View Post
    Is this what you want to do: Convert each character in a String to a String of 8 binary digits.
    For example: A String of 3 characters would be converted to a String of 24 characters of (0 and 1s).
    yes.
    I'm at work now, when I get back home I'll explain why I need this.

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default Re: String to binary

    What I said would only be true for ASCII characters. Unicode characters take two bytes which would product 16 binary digit characters.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Re: String to binary

    I want to create some kind of a winrar.
    Now I thought how can I zip and reduce file size, so I thought if I could convert a file into binary and do this:
    binary example: 10011110001
    ziped: 12431 // first number represent the "1" sequence, second represent the "0" sequence in the binary and so on.
    1 = 1
    2 = 00
    4 = 1111
    3 = 000
    1 = 1
    Then I get back my 10011110001

    I wanted to convert it from string because usually I read files into a string.
    Now when everything is more clear I hope you can help me.

    Thanks guys.

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default Re: String to binary

    How many bits does it take to store: 10011110001
    It looks like about 11 bits
    How many bits does it take to store: 12431?
    If each digit takes 8 bits, then that would take 40 bits.
    Not much savings converting 11 bits to 40 bits.
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Re: String to binary

    Ok, I understand.
    But I'm sure that the solution of decreasing file size would come from binary and even if not I want to know how to do it.
    Thanks.

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default Re: String to binary

    I want to know how to do it.
    How to do what?
    If you don't understand my response, don't ignore it, ask a question.

  17. #17
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: String to binary

    Have you studied how existing compression algorithms work?
    I would suggest looking at that before trying to come up with your own.
    When you understand what they do then you'll be in a better position to roll your own.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. How To change String to binary.
    By Khan05 in forum New To Java
    Replies: 16
    Last Post: 07-20-2011, 05:21 PM
  2. converting binary to string
    By stegano in forum New To Java
    Replies: 5
    Last Post: 03-27-2011, 08:20 PM
  3. Convert String to Binary equivalent
    By vinayak.top in forum Advanced Java
    Replies: 1
    Last Post: 01-21-2011, 09:07 AM
  4. Replies: 12
    Last Post: 11-26-2010, 09:33 AM
  5. Convert String to Binary
    By erakhman in forum New To Java
    Replies: 1
    Last Post: 09-01-2010, 09:25 AM

Posting Permissions

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