Results 1 to 3 of 3
  1. #1
    RobertF is offline Member
    Join Date
    Mar 2009
    Posts
    13
    Rep Power
    0

    Default split() by digits

    Hey,
    I've been trying to split binary into separate 1's and 0's and store them into an array. I've been searching for ages for the correct regex to do this and just can't find it anywhere! I need to split the following:

    10111

    into:

    result[0] = 1
    result[1] = 0
    result[2] = 1
    result[3] = 1
    result[4] = 1

    I appreciate any help!
    Thanks =]

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

    Default

    Proabaly best to work with String representations of binary numbers. Then use the appropriate classes in String and Integer to do the dirty work:

    Java Code:
    public class Fubar
    {
      public static void main(String[] args)
      {
        int binaryNo = Integer.parseInt("10111", 2);
        System.out.println(binaryNo);
        System.out.println(Integer.toBinaryString(binaryNo));
        
        char[] binaryArray = Integer.toBinaryString(binaryNo).toCharArray();
        
        for (char c : binaryArray)
        {
          System.out.println(c);
        }
        
      }
    
    }

  3. #3
    RobertF is offline Member
    Join Date
    Mar 2009
    Posts
    13
    Rep Power
    0

Similar Threads

  1. How to split a String using split function
    By Java Tip in forum java.lang
    Replies: 4
    Last Post: 04-17-2009, 08:27 PM
  2. sum of digits depreciation
    By jleas in forum New To Java
    Replies: 13
    Last Post: 11-09-2008, 01:37 PM
  3. how to split a file
    By nagaraaju in forum New To Java
    Replies: 0
    Last Post: 03-14-2008, 08:45 AM
  4. How to split a String using split function
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 09:32 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
  •