Java Byte Array Create toByteArrayBE(byte b)

Here you can find the source of toByteArrayBE(byte b)

Description

to Byte Array BE

License

Open Source License

Declaration

public static byte[] toByteArrayBE(byte b) 

Method Source Code

//package com.java2s;
/*-/*from   w  w w . j a v  a 2  s.  c  om*/
 * Copyright (C) 2006-2007 Erik Larsson
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    public static byte[] toByteArrayBE(byte b) {
        byte[] result = new byte[1];
        result[0] = b;
        return result;
    }

    public static byte[] toByteArrayBE(short s) {
        byte[] result = new byte[2];
        result[0] = (byte) ((s >> 8) & 0xFF);
        result[1] = (byte) ((s >> 0) & 0xFF);
        return result;
    }

    public static byte[] toByteArrayBE(char c) {
        byte[] result = new byte[2];
        result[0] = (byte) ((c >> 8) & 0xFF);
        result[1] = (byte) ((c >> 0) & 0xFF);
        return result;
    }

    public static byte[] toByteArrayBE(int i) {
        byte[] result = new byte[4];
        result[0] = (byte) ((i >> 24) & 0xFF);
        result[1] = (byte) ((i >> 16) & 0xFF);
        result[2] = (byte) ((i >> 8) & 0xFF);
        result[3] = (byte) ((i >> 0) & 0xFF);
        return result;
    }

    public static byte[] toByteArrayBE(long l) {
        byte[] result = new byte[8];
        result[0] = (byte) ((l >> 56) & 0xFF);
        result[1] = (byte) ((l >> 48) & 0xFF);
        result[2] = (byte) ((l >> 40) & 0xFF);
        result[3] = (byte) ((l >> 32) & 0xFF);
        result[4] = (byte) ((l >> 24) & 0xFF);
        result[5] = (byte) ((l >> 16) & 0xFF);
        result[6] = (byte) ((l >> 8) & 0xFF);
        result[7] = (byte) ((l >> 0) & 0xFF);
        return result;
    }
}

Related

  1. toByteArray(String strTransInfo)
  2. toByteArray(String uid)
  3. toByteArray(String value)
  4. toByteArray(String[] anArray)
  5. toByteArray1D(int[][] d)
  6. toByteArrayForPBE(char[] chars)
  7. toByteArrayFromPositiveInts(int[] inInts)
  8. toByteArrayFromString(String value, int radix)
  9. toByteArrayLE(int value)