Java tutorial
//package com.java2s; //License from project: Open Source License public class Main { private final static short MAX_UNSIGNED_BYTE_VALUE = 256; private final static int MAX_UNSIGNED_SHORT_VALUE = 65536; public static byte ushortToByte(short value) { if (value <= MAX_UNSIGNED_SHORT_VALUE) { if (value >= MAX_UNSIGNED_BYTE_VALUE / 2) { return (byte) ((~(MAX_UNSIGNED_BYTE_VALUE - value)) + 1); } else { return (byte) value; } } else { throw new IllegalArgumentException("Value out of range for a byte"); } } }