Results 1 to 3 of 3
  1. #1
    rushenas is offline Member
    Join Date
    Jun 2008
    Posts
    2
    Rep Power
    0

    Default get binary value

    Hi all
    Consider that I have a string in hex like this:
    Java Code:
    3020058020C00004
    Now I wanna convert it to binary, then I wanna have each digit single. For example, binary value for above string is:
    Java Code:
    11000000100000000001011000000000100000110000000000000000000100
    And I wanna have
    Java Code:
    1
    1
    0
    0
    0
    0
    0
    0
    1
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    1
    0
    1
    1
    0
    0
    0
    0
    0
    0
    0
    0
    0
    1
    0
    0
    0
    0
    0
    1
    1
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    1
    0
    0
    How can I achieve this?

  2. #2
    danielstoner's Avatar
    danielstoner is offline Senior Member
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    191
    Rep Power
    7

    Default

    You can use:

    Java Code:
    // Long: static long parseLong(String s, int radix)
    long l = Long.parseLong("3020058020C00004", 16);
    
    // Long: static String toBinaryString(long i)
    String b = Long.toBinaryString(l);
    
    // String: char charAt(int index)
    for (int i = 0; i < b.length(); i++)
    {
        System.out.println(b.charAt(i));
    }
    to parse the input String to a long and then to transform it into a binary string. In th end you display the digits one by one.
    Daniel @ [www.littletutorials.com]
    Language is froth on the surface of thought

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. binary conversion
    By thamizhisai in forum New To Java
    Replies: 3
    Last Post: 04-29-2008, 12:55 PM
  2. Binary Addition
    By Deo Favente in forum Advanced Java
    Replies: 11
    Last Post: 04-24-2008, 06:34 AM
  3. Binary Search in Java
    By Java Tip in forum Algorithms
    Replies: 0
    Last Post: 04-15-2008, 08:43 PM
  4. JSP- Binary output stream
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-29-2008, 10:06 AM
  5. binary search
    By tranceluv in forum New To Java
    Replies: 10
    Last Post: 01-14-2008, 08:13 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
  •