0

I have an integer array

int res[] = {176, 192, 312, 1028, 1064, 1016};

I get the signed byte array of the corresponding int array like this

int signed_byte_array[] = {-80, 0, -64, 0, 56, 1, 4, 4, 40, 4, -8, 3};

Each index in the int array is represented by two indexes in the byte array, means each value in int array is represented as 2 bytes.

I don't have access to the int array and I want to convert this signed byte array exactly to the int array

How can I do this?

Thanks

7
  • 1
    what? could you please explain more thoroughly? Commented Jun 21, 2013 at 10:03
  • possible duplicate of stackoverflow.com/questions/11437203/byte-array-to-int-array Commented Jun 21, 2013 at 10:05
  • I have the signed byte array and i want to convert into int array, just consider the above byte array as input and convert it to int array so tht it gives me the above integer array Commented Jun 21, 2013 at 10:05
  • "each value in int array is represented as 2 bytes" - what would you want to do with an integer value more than 2^16? Commented Jun 21, 2013 at 10:05
  • depends on the signal value it can be anything larger Commented Jun 21, 2013 at 10:06

2 Answers 2

1

It appears that the bytes represent unsigned 16-bit integers, with the most significant byte coming second, and the bits above 8-th truncated. You can do the conversion like this:

int[] signed_byte_array = {-80, 0, -64, 0, 56, 1, 4, 4, 40, 4, -8, 3};
int[] int_array = new int[signed_byte_array.length / 2];
for (int i = 0 ; i != int_array.length ; i++) {
    int_array[i] = (signed_byte_array[2*i+1] & 0xFF) << 8
                 | (signed_byte_array[2*i+0] & 0xFF);
}

When I added printing of int_array[i] in the loop I got these values:

176 192 312 1028 1064 1016
Sign up to request clarification or add additional context in comments.

Comments

0
private static int[] toB(int[] res) {
    int[] bytes = new int[res.length * 2];
    for (int i = 0; i < res.length; ++i) {
        int value = res[i];
        int lo = value & 0xFF;
        int hi = (value >> 8) & 0xFF;
        bytes[2 * i] = (int)(byte) lo;
        bytes[2 * i + 1] = (int)(byte) hi;
    }
    return bytes;
}

private static int[] toI(int[] bytes) {
    int[] res = new int[bytes.length / 2];
    for (int i = 0; i < res.length; ++i) {
        int lo = (int) bytes[2 * i] & 0xFF;
        int hi = (int) bytes[2 * i + 1] & 0xFF;
        res[i] = lo | (hi << 8);
    }
    return res;
}

public static void main(String[] args) {
    int[] res = {176, 192, 312, 1028, 1064, 1016};
    int[] signed_byte_array = {-80, 0, -64, 0, 56, 1, 4, 4, 40, 4, -8, 3};
    System.out.println("b: " + Arrays.toString(toB(res)));
    System.out.println("i: " + Arrays.toString(toI(signed_byte_array)));
}

gives

b: [-80, 0, -64, 0, 56, 1, 4, 4, 40, 4, -8, 3]
i: [176, 192, 312, 1028, 1064, 1016]

It is evidently a short[] to byte[] conversion. As you have an int[] I would say, there exists no utility conversion function.

3 Comments

Joop Eggen How can I take lo and hi bytes as one integer value and save it as an array index
Sorry did a save on the first half. Now complete. Otherwise hard to test automatically.
@UmairIqbal made it executable. Some casts are too much as I changed byte to the used int.

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.