Java Integer to intTo2UnsignedBytes(int val)

Here you can find the source of intTo2UnsignedBytes(int val)

Description

This method receives an signed int and converts it to the equivalent unsigned byte values of the two least significant bytes of the int.

License

Open Source License

Parameter

Parameter Description
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.

Declaration

public synchronized static byte[] intTo2UnsignedBytes(int val) 

Method Source Code

//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;
    }
}

Related

  1. int2rgb(final int color)
  2. Int2Short(int i)
  3. int2sortableStr(int val)
  4. int2TwoChars(int i)
  5. intTo2LengthBytes(int i)
  6. intTo8HexString(int i)
  7. intToAlgebraicLoc(int loc)
  8. intToAlpha(Integer no)
  9. intToASN1(byte[] d, int idx, int val)