Java Byte Array Create toBytes(short value)

Here you can find the source of toBytes(short value)

Description

to Bytes

License

Apache License

Declaration

public static final byte[] toBytes(short value) 

Method Source Code

//package com.java2s;
/*//from   w w  w .  j  a va  2  s. c om
 * Copyright 2013 Lyor Goldstein
 * 
 * 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 final byte[] toBytes(short value) {
        byte[] buf = new byte[2];
        toBytes(value, buf);
        return buf;
    }

    public static final int toBytes(short value, byte[] buf) {
        return toBytes(value, buf, 0, buf.length);
    }

    public static final int toBytes(short value, byte[] buf, int offset, int availableLen) {
        if (availableLen < 2) {
            throw new IllegalArgumentException("Insufficient available length: " + availableLen);
        }

        for (int index = 0, mask = value, pos = offset; index < 2; index++, pos++, mask <<= Byte.SIZE) {
            buf[pos] = (byte) ((mask >> 8) & 0xFF);
        }

        return 2;
    }

    public static final byte[] toBytes(int value) {
        byte[] buf = new byte[4];
        toBytes(value, buf);
        return buf;
    }

    public static final int toBytes(int value, byte[] buf) {
        return toBytes(value, buf, 0, buf.length);
    }

    public static final int toBytes(int value, byte[] buf, int offset, int availableLen) {
        if (availableLen < 4) {
            throw new IllegalArgumentException("Insufficient available length: " + availableLen);
        }

        for (int index = 0, mask = value, pos = offset; index < 4; index++, pos++, mask <<= Byte.SIZE) {
            buf[pos] = (byte) ((mask >> 24) & 0xFF);
        }

        return 4;
    }

    public static final byte[] toBytes(long value) {
        byte[] buf = new byte[8];
        toBytes(value, buf);
        return buf;
    }

    public static final int toBytes(long value, byte[] buf) {
        return toBytes(value, buf, 0, buf.length);
    }

    public static final int toBytes(long value, byte[] buf, int offset, int availableLen) {
        if (availableLen < 8) {
            throw new IllegalArgumentException("Insufficient available length: " + availableLen);
        }

        long mask = value;
        for (int index = 0, pos = offset; index < 8; index++, pos++, mask <<= Byte.SIZE) {
            buf[pos] = (byte) ((mask >> 56) & 0xFF);
        }

        return 8;
    }
}

Related

  1. toBytes(Object value)
  2. toBytes(short s)
  3. toBytes(short s)
  4. toBytes(short sVal, byte[] bytes, boolean bigEndian)
  5. toBytes(short value)
  6. toBytes(String hex)
  7. toBytes(String hex)
  8. toBytes(String hex)
  9. toBytes(String hex)