Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The other answer is correct, you are not taking into account Java's use of signed bytes.</p> <p>There are a few possibilities for solving this:</p> <ol> <li>You can do the stuff above with "&amp; 0xFF"</li> <li>You can just treat everything as ints (as I have done below)</li> <li>You could use a library to help. Some examples are <a href="http://code.google.com/p/joou/" rel="nofollow">JOOU</a> (which was started in response to this lack of unsigned types) or <a href="http://netty.io/" rel="nofollow">netty channelbuffers</a> (NB: although netty is focussed on network IO, like sockets etc., its channel buffer class is great for dealing with bytestreams and signed/unsigned types of different lengths and I have used it quite a lot for transforming binary protocols like the one you're handling.)</li> </ol> <p>I have written a "solution" to your question below.</p> <pre><code>public class SOAnswer { private static void vbusExtractSeptett(int[] data, int offset, int length) { int septett = 0; for (int i = 0; i &lt; length; i++) { if ((data[offset + i] &amp; 0x80) != 0) { data[offset + i] &amp;= 0x7F; septett |= 1 &lt;&lt; i; } } data[offset + length] = septett; } private static void vbusInjectSeptett(final int[] data, int offset, int length) { int septett = data[offset + length]; for (int i = 0; i &lt; length; i++) { if ((septett &amp; (1 &lt;&lt; i)) != 0) data[offset + i] |= 0x80; } // clear the septett byte data[offset + length] = 0x00; } private static void printIntArrayAsHEX(int[] array) { StringBuilder builder = new StringBuilder(); for ( int a : array ) { String s = Integer.toHexString( a ); if (s.length() == 1) builder.append( "0" ); builder.append(s + ":"); } builder.substring( 0, builder.lastIndexOf( ":" ) - 1 ); System.out.println(builder.toString()); } public static void main( String[] args ) { // Create an array long enough for the extracting/injecting int[] arr = new int[]{0x2E, 0x00, 0xDF, 0x00, 0x00}; // see what it looks like printIntArrayAsHEX(arr); // perform extraction vbusExtractSeptett( arr, 0, 4 ); // see what it looks like printIntArrayAsHEX(arr); // Perform injection vbusInjectSeptett( arr, 0, 4 ); // see what it looks like printIntArrayAsHEX(arr); } } </code></pre> <p>One recommendation I would have for you is to think about if you really want to to re-implement the C code verbatim (especially the very functional programming style of passing an array of primitive types, an offset into the array and the length of the array). Maybe something more like this would be more OO:</p> <pre><code>private static int[] vbusExtractSeptettJAVASTYLE(int[] data) { int[] extractedData = Arrays.copyOf( data, data.length +1 ); int septett = 0; for (int i = 0; i &lt; data.length; i++) { if ((data[i] &amp; 0x80) != 0) { extractedData[i] = data[i] &amp;= 0x7F; septett |= 1 &lt;&lt; i; } } extractedData[extractedData.length-1] = septett; return extractedData; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload