Java Integer to intToTwoBytes(int value)

Here you can find the source of intToTwoBytes(int value)

Description

This function converts integer to two bytes

License

Open Source License

Parameter

Parameter Description
value int type

Exception

Parameter Description
Exception Exception

Return

value int type

Declaration

public static final byte[] intToTwoBytes(int value) throws Exception 

Method Source Code

//package com.java2s;
/*/*from w  w w. ja  v  a 2 s.  c  om*/
 * Copyright (c) 2016 Tata Consultancy Services and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */

public class Main {
    /**
     * This function converts integer to two bytes
     * 
     * @param value
     *            int type
     * @return value int type
     * @throws Exception
     *             Exception
     */
    public static final byte[] intToTwoBytes(int value) throws Exception {
        byte[] result = new byte[2];
        if ((value > Math.pow(2, 31)) || (value < 0)) {
            throw new Exception("Integer value " + value + " is larger than 2^31");
        }
        result[0] = (byte) ((value >>> 8) & 0xFF);
        result[1] = (byte) (value & 0xFF);
        return result;
    }
}

Related

  1. intToStringWithZeroFill(int intValue, int width)
  2. intToTime(int time)
  3. intToTime(int value)
  4. intToTriplePlace(int i)
  5. intToTwoByte(int value, byte[] destination, int offset)
  6. intToTwoDigitString(int integer)
  7. intToTwoHexString(final int value)
  8. intToUByte(int i)
  9. intToULong(int signed)