Byte to short conversionFrom WikiJavabuy this book
Sometimes it's necessary to convert from bytes to shorts. Have you ever tried to store a video coordinate? The shorts are the solutions.
the code/** * @param left * The most valued byte. * @param right * The less valued byte. * @return A short mixing the two parts. */ private static short byte2short(final byte left, final byte right) { return (short) ((left & 0xff) << 8 | right & 0xff); } As you can see we move the left one to the left, and "add" using the OR (|) operator with the right one. The bitmask 0xff is necessary to "hide" the other part of the number. Any comment is appreciated. |
