3

Every time i come across the value "-15" i want to put it in a new byte array... how would i do this? I think i'm just tired and getting way to confused and worked up... please help... code is as follows:

    fileContent = "S107184CB78120EA52S107184CB78120EA57"; 
    int len = fileContent.length(); 
    byte[] data = new byte[len/2]; 
    for (int i = 0; i < len; i += 2) 
    {
        data[i / 2] = (byte) ((Character.digit(fileContent.charAt(i), 16) << 4) + Character.digit(fileContent.charAt(i+1), 16));
    }

this will give me the following: [-15, 7, 24, 76, -73, -127, 32, -22, 82, -15, 7, 24, 76, -73, -127, 32, -22, 87]

what i want from here is to have every element from -15 up until the next -15 (but not including it) in a separate byte array. i want to do that every time i come across another -15.. how would i do this?

9
  • Iterate over the array, and every time you see a -15, create a new array and following elements to it. Commented Mar 6, 2012 at 0:45
  • possible duplicate of Splitting a Byte array Commented Mar 6, 2012 at 0:49
  • (Not an exact duplicate, but if the OP can't adapt one of those answers, he's got much bigger problems ...) Commented Mar 6, 2012 at 0:51
  • @StephenC you mean SHE.. and i've just looked at it now.... who said i wouldn't accept an answer?! Commented Mar 6, 2012 at 1:52
  • @StephenC it's not a duplicate either... Commented Mar 6, 2012 at 1:53

1 Answer 1

3

Assuming -15's occur randomly, you need a dynamic array, which in java is a List. Once you've collected up all your elements, you can convert the List to an array easily enough.

It would look something like this:

String fileContent = "S107184CB78120EA52S107184CB78120EA57";
int len = fileContent.length();
List<Byte[]> arrays = new ArrayList<Byte[]>();
List<Byte> data = new ArrayList<Byte>();
for (int i = 0; i < len; i += 2) {
    byte b = (byte) ((Character.digit(fileContent.charAt(i), 16) << 4) + Character.digit(fileContent.charAt(i + 1), 16));
    if (b == -15) {
        arrays.add(data.toArray(new Byte[data.size()]));
        data.clear();
    }
    data.add(b);
}
arrays.add(data.toArray(new Byte[data.size()]));

Note that this creates a List<Byte[]>, not a List<byte[]>. I chose this because I could use the .toArray() method of List. If you want a List<byte[]>, you'll need to manually literate over data in the if block.

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, but you can do this a lot more simply and efficiently using Arrays.copyOfRange() - see link to earlier question. Of course, this is irrelevant to the OP if she just wants something she can copy and paste. But there are other people out there who might care about this.
@StephenC If I could give you rep for a comment I would. Good tip!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.