Java Convert via ByteBuffer toByteArrayFromInt(int intValue, boolean shortSize)

Here you can find the source of toByteArrayFromInt(int intValue, boolean shortSize)

Description

Converts an integer (which in Java is a signed four bytes value) into a byte array

License

Open Source License

Parameter

Parameter Description
intValue The integer value which is to be converted to a byte array
shortSize Determines if the integer value shall be represented as a 4-byte or a 2-byte array. Example: A port number between 49152 and 65535 for UDP client or TCP server is represented by a (signed) integer value with 4 bytes in Java. The SECCDiscoveryResponse, however, holds only two bytes for the port, thus the leading 16 bytes consisting of 0s must be cut off.

Return

The byte array corresponding to the given integer value, either with a size of 2 or 4 bytes.

Declaration

public static byte[] toByteArrayFromInt(int intValue, boolean shortSize) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 *  Copyright (c) 2016 Dr.-Ing. Marc M?ltin.
 *  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
 *
 *  Contributors:/*w  w  w.  j a v a2s.  c o  m*/
 *    Dr.-Ing. Marc M?ltin - initial API and implementation and initial documentation
 *******************************************************************************/

import java.nio.ByteBuffer;

public class Main {
    /**
     * Converts an integer (which in Java is a signed four bytes value) into a byte array
     * @param intValue The integer value which is to be converted to a byte array
     * @param shortSize Determines if the integer value shall be represented as a 4-byte or a 2-byte array.
     *         Example: A port number between 49152 and 65535 for UDP client or TCP server is represented
     *         by a (signed) integer value with 4 bytes in Java. The SECCDiscoveryResponse, however, holds
     *         only two bytes for the port, thus the leading 16 bytes consisting of 0s must be cut off. 
     * @return The byte array corresponding to the given integer value, either with a size of 2 or 4 bytes.
     */
    public static byte[] toByteArrayFromInt(int intValue, boolean shortSize) {
        byte[] tempArr = ByteBuffer.allocate(4).putInt(intValue).array();
        byte[] retArr;

        if (shortSize) {
            retArr = ByteBuffer.allocate(2).put(tempArr, 2, 2).array();
        } else {
            retArr = tempArr;
        }

        return retArr;
    }
}

Related

  1. ToByteArray(String hexString)
  2. toByteArray(String value)
  3. toByteArray(UUID uniqueId)
  4. toByteArray2(String filename)
  5. toByteArray3(String filename)
  6. toByteArrayFromLong(long longValue)
  7. toBytes(BigDecimal number, int byteLength)
  8. toBytes(char[] ch)
  9. toBytes(char[] chars)