Java Long to Byte long2Byte(byte[] bytes, long value, int offset)

Here you can find the source of long2Byte(byte[] bytes, long value, int offset)

Description

long Byte

License

Apache License

Declaration

public static void long2Byte(byte[] bytes, long value, int offset) 

Method Source Code

//package com.java2s;
/*/* ww w .  j a  v a2 s  .  co  m*/
 * Copyright 2015-2017 GenerallyCloud.com
 *  
 * 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.
 */

public class Main {
    public static void long2Byte(byte[] bytes, long value, int offset) {

        checkLength(bytes, 8, offset);

        bytes[offset + 7] = (byte) ((value & 0xff));
        bytes[offset + 6] = (byte) ((value >> 8 * 1) & 0xff);
        bytes[offset + 5] = (byte) ((value >> 8 * 2) & 0xff);
        bytes[offset + 4] = (byte) ((value >> 8 * 3) & 0xff);
        bytes[offset + 3] = (byte) ((value >> 8 * 4) & 0xff);
        bytes[offset + 2] = (byte) ((value >> 8 * 5) & 0xff);
        bytes[offset + 1] = (byte) ((value >> 8 * 6) & 0xff);
        bytes[offset + 0] = (byte) ((value >> 8 * 7));

    }

    private static void checkLength(byte[] bytes, int length, int offset) {

        if (bytes == null) {
            throw new IllegalArgumentException("null");
        }

        if (offset < 0) {
            throw new IllegalArgumentException("invalidate offset " + offset);
        }

        if (bytes.length - offset < length) {
            throw new IllegalArgumentException("invalidate length " + bytes.length);
        }
    }
}

Related

  1. long2byte(byte[] b, long a)
  2. Long2Byte(long i)
  3. long2byte(long ival, byte b[], int offset)
  4. long2byte(long l)
  5. long2byte(long l)