Here you can find the source of intToTwoBytes(int value)
Parameter | Description |
---|---|
value | int type |
Parameter | Description |
---|---|
Exception | Exception |
public static final byte[] intToTwoBytes(int value) throws Exception
//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; } }