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