2

I have a text file that contains "binary", which is simply just a long string of binary numbers that will represent assembly instructions further down the road. At the moment what I want to do is take this long string of binary numbers and load it into a String[] 8 characters at a time.

So for example: 0000000100000002000000030000000400000005000000060000000700000008

Would be read in as:

[0] 000000001
[1] 000000002
[2] 000000003
[3] 000000004
[4] 000000005
[5] 000000006
[6] 000000007
[7] 000000008

The only way I can think about doing this right now is by reading in the entire string and storing it in a String variable, then iterating through the variable 8 chars at a time and using substring() to slice the big string into smaller 8 char strings.

Surely there must be a more efficient way of doing this?

3 Answers3

2
  1. You can use regex provided by Alan Moore in this answer and if you prefer Java 8 solution, you might want to consider using Stream:

    String str = "0000000100000002000000030000000400000005000000060000000700000008";
    List<String> list = (Stream.of(str.split("(?<=\\G.{8})"))
                         .collect(Collectors.toList())); // Result of splitting input String is stored in a List
    String[] strings = list.toArray(new String[list.size()]); // Create an array from contents of list
    System.out.println(Arrays.toString(strings)); // print result array to console
    
  2. If you are not allowed to use Java 8 features, you can use method substring() provided in String class:

    String str = "0000000100000002000000030000000400000005000000060000000700000008";
    ArrayList<String> list = new ArrayList<>(); // To store results of cutting input String
    while(str.length() != 0) { // Until you reach end of String...
        list.add(str.substring(0, 8)); // Add first eight characters of input String
        str = str.substring(8, str.length()); // Cut input String to leave only characters not added to list in previous line
    }
    String[] array = list.toArray(new String[list.size()]); // Create a String[] and add contents of ArrayList to it
    System.out.println(Arrays.toString(array)); // Print result String[]
    

Output you get in both cases:

[00000001, 00000002, 00000003, 00000004, 00000005, 00000006, 00000007, 00000008]
Przemysław Moskal
  • 3,551
  • 2
  • 12
  • 21
0

You can create a Scanner and keep "scanning" the next 8 characters:

Scanner s = new Scanner(new File(...));
// you cannot use array here since you don't know how long the file is
// you can convert this array list to an array afterwards though.
ArrayList<String> list = new ArrayList<>();
String eightChars;
while ((eightChars = s.findWithinHorizon(".{8}", 0)) == null) {
    list.add(eightChars);
}

.{8} is a regular expression that matches exactly 8 non-line-terminator characters.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
0

You can have a byte buffer of a 8 bytes so that each read can only take up to 8 characters. You can form a String from this buffer easily using the String constructor.

    char c[] = new char[8];
    List<String> strings = new ArrayList<>();
    try (FileReader reader = new FileReader(new File(""))) {
        while (reader.read(c) != -1) {
            strings.add(new String(c));
        }
    } catch (Exception e) {
        //Handle exception
    }
Zachary
  • 1,693
  • 1
  • 9
  • 13