Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Java has no unsigned numbers (<code>char</code> is 16bit unsigned but it's not a number and math with <code>char</code> will always result in implicit casts to <code>int</code>)</p> <p>If you read 2 bytes of unsigned data into a <code>short</code> and want to see values in range from 0-65535 (instead of -32768 - 32767) you'll have to use a type that can have values in that range.</p> <p>In case of 16bit <code>short</code> next bigger one is 32bit <code>int</code>. The conversion that does the trick is</p> <pre><code>short signed = ...; int unsigned = signed &amp; 0xFFFF; </code></pre> <p>Assuming <code>signed</code> has a value of <code>0xFFFF</code> this is what happens:</p> <pre><code>short signed = -1; // FFFF on byte level </code></pre> <p>The expression <code>signed &amp; 0xFFFF</code> contains a <code>short</code> and an <code>int</code>. <code>0xFFFF</code> is a literal integer type number which when found in Java source is considered <code>int</code>. You could make it <code>long</code> by changing it to <code>0xFFFFL</code> (you would need that if you want to convert unsigned <code>int</code> to <code>long</code>).</p> <p>Since the <code>&amp;</code> operator needs both sides in a common type Java will silently convert the smaller one.</p> <pre><code>int stillSigned = (int) signed; // hidden step </code></pre> <p>It will still have the exact same value of -1 since that's what it was before when looking at it unsigned but it is changed on bytelevel to <code>0xFFFFFFFF</code>.</p> <p>Now the bit-manipulation is applied to remove all the added <code>FF</code>s</p> <pre><code>int unsigned = stillSigned &amp; 0xFFFF; </code></pre> <p>and you end up with <code>0x0000FFFF</code> on byte level and can finally see the value of 65535.</p> <p>Since you happen to have 16bit values you can use <code>char</code> and simply cast it to <code>int</code>.</p> <pre><code>char value = ...; int unsigned = value; </code></pre> <p>But above approach works for any unsigned conversion: <code>byteValue &amp; 0xFF</code>, <code>shortValue &amp; 0xFFFF</code>, <code>intValue &amp; 0xFFFFFFFFL</code></p> <hr> <p>The next thing you should do is not to use a simple <code>InputStream</code> to do</p> <pre><code>SerialPort device = SerialPort(file, baud, flags); InputStream in = device.getInputStream(); byte[] buffer = new byte[384]; in.read(buffer); </code></pre> <p>Reason is that <a href="http://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#read%28byte%5b%5d%29" rel="nofollow">InputStream#read(byte[])</a> is not guaranteed to read all the bytes you want in your buffer. It returns you the number of bytes it has read or <code>-1</code> if the stream is done. Manually writing code that ensures you have a filled buffer is nasty but there is a simple solution: <a href="http://docs.oracle.com/javase/7/docs/api/java/io/DataInputStream.html" rel="nofollow">DataInputStream</a></p> <pre><code>SerialPort device = SerialPort(file, baud, flags); DataInputStream in = new DataInputStream(device.getInputStream()); byte[] buffer = new byte[384]; in.readFully(buffer); </code></pre> <p><code>DataInputStream</code> has very nice functionality that you could use:</p> <pre><code>SerialPort device = SerialPort(file, baud, flags); DataInputStream in = new DataInputStream(device.getInputStream()); int unsignedShort = in.readUnsignedShort(); </code></pre> <p>Another way to get different numbers out of <code>byte[]</code> data is to use <code>ByteBuffer</code> since that provides methods like <code>.getShort()</code></p> <pre><code>SerialPort device = SerialPort(file, baud, flags); DataInputStream in = new DataInputStream(device.getInputStream()); byte[] buffer = new byte[384]; in.readFully(buffer); ByteBuffer byteBuffer = ByteBuffer.wrap(buffer); while (byteBuffer.hasRemaining()) { int unsigned = byteBuffer.getChar(); System.out.println(unsigned); } </code></pre>
    singulars
    1. This table or related slice is empty.
    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. VO
      singulars
      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