Java ByteOrder setLong(byte[] bytes, int start, int end, long value, ByteOrder byteOrder)

Here you can find the source of setLong(byte[] bytes, int start, int end, long value, ByteOrder byteOrder)

Description

set a long value in a byte array

License

Apache License

Parameter

Parameter Description
bytes the byte array
start starting index position inside byte array
end ending position inside byte array
value the value
byteOrder byte order

Declaration

public static byte[] setLong(byte[] bytes, int start, int end, long value, ByteOrder byteOrder) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2015 alladin-IT GmbH// ww  w.j a v a2s  .c om
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/

import java.nio.ByteOrder;

public class Main {
    /**
     * set a long value in a byte array 
     * @param bytes the byte array
     * @param start starting index position inside byte array
     * @param end ending position inside byte array
     * @param value the value
     * @param byteOrder byte order
     * @return
     */
    public static byte[] setLong(byte[] bytes, int start, int end, long value, ByteOrder byteOrder) {
        for (int n = 0; n <= (end - start); n++) {
            byte b = (byte) (value % 256);
            bytes[ByteOrder.BIG_ENDIAN.equals(byteOrder) ? (end - start) - (n - start) : n + start] = b;
            value >>= 8;
        }

        return bytes;
    }
}

Related

  1. increaseNumberOfBytes(byte[] originalBytes, int desiredNumberOfBytes, ByteOrder byteOrder, boolean isSigned)
  2. isNegative(byte[] bytes, ByteOrder byteOrder, boolean isSigned)
  3. longToBytes(long longValue, ByteOrder byteOrder, boolean isSigned)
  4. opposite(ByteOrder order)
  5. reduceToSmallestByteArray(byte[] originalBytes, ByteOrder byteOrder, boolean isSigned)
  6. toBytes(int value, ByteOrder order)
  7. toDoubleByteArray(double val, ByteOrder outOrder, byte[] buf, int off)
  8. toFloatByteArray(float val, ByteOrder outOrder, byte[] buf, int off)
  9. toInt(byte[] bytes, ByteOrder order)