Convert long [64] to byte [512] in Java?

I port the process to Java. There are already working versions in C # and C ++.

I have a section in C # that I am doing Marshal.Copy (...) to convert 64 ulongs to 512 bytes, and this C ++ line I use memmove (...) to do the same. What is available in Java to achieve the same result? I need the same binary information in the same order as bytes instead of longs.

Edit:

The reason I'm porting to Java is to use the portability that Java naturally has. I would not want to use my own code.

Another thing. Since Java does not contain unsigned values, I need to slightly modify what I request. I would like to get 8 unsigned byte values ​​from each of 64 lengths (ulongs in C # and C ++), to subsequently use these values ​​in indexes in arrays. This has to happen thousands of times, so the fastest way is the best way.

+16
java porting
Feb 06 2018-10-06T00
source share
1 answer

ByteBuffer works well for this: just enter 64 long values ​​and get byte[] using the array() method. The ByteOrder class can handle end problems efficiently. For example, the inclusion of the approach suggested in the wierob comment:

 private static byte[] xform(long[] la, ByteOrder order) { ByteBuffer bb = ByteBuffer.allocate(la.length * 8); bb.order(order); bb.asLongBuffer().put(la); return bb.array(); } 

Addendum: the resulting byte[] components are signed 8-bit values, but Java arrays require non-negative integer values . Marking a byte into int will expand the sign, but when masking a bit of a higher order, an unsigned byte b value will be byte b :

 int i = (int) b & 0xFF; 

This answer describes the current operator precedence rules. This related answer demonstrates a similar approach for double values.

+20
Feb 06 '10 at 4:13
source share



All Articles