Here you can find the source of intTo2UnsignedBytes(int val)
Parameter | Description |
---|---|
val | an signed integer value 0 <= val <= 2^16 - 1 |
public synchronized static byte[] intTo2UnsignedBytes(int val)
//package com.java2s; //License from project: Open Source License public class Main { /**// ww w. ja v a 2 s. c o m * This method receives an signed int and converts it to the equivalent * unsigned byte values of the two least significant bytes of the int. * @param val an signed integer value 0 <= val <= 2^16 - 1 * @return byte[] containing the unsigned equivalent of the entered * value of the integer to the method. The least significant bits in * index 1 and the most significant bits in index 0. * */ public synchronized static byte[] intTo2UnsignedBytes(int val) { int upperBound = (int) Math.round(Math.pow(2, 16) - 1); int lowerBound = 0; if (val < lowerBound || val > upperBound) { throw new IllegalArgumentException( "Argument has to be 0 <= val <= (2^16-1)"); } int no256s = val / 256; byte[] out = new byte[2]; out[0] = (byte) no256s; out[1] = (byte) (val % 256); return out; } }