Java Byte Array Create toBytes(int n)

Here you can find the source of toBytes(int n)

Description

Converter

License

Open Source License

Parameter

Parameter Description
n a parameter

Declaration

public static byte[] toBytes(int n) 

Method Source Code

//package com.java2s;
/*// w ww.  ja va2 s  . c  om
 * Copyright 05-abr-2014 ?Deme
 *
 * This file is part of 'dmLang-8.1.0'.
 *
 * 'dmLang-8.1.0' 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.
 *
 * 'dmLang-8.1.0' 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 'dmLang-8.1.0'.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Converter
     *
     * @param n
     * @return
     */
    public static byte[] toBytes(int n) {
        byte[] r = new byte[4];
        r[0] = (byte) (n % 256);
        n /= 256;
        r[1] = (byte) (n % 256);
        n /= 256;
        r[2] = (byte) (n % 256);
        r[3] = (byte) (n / 256);
        return r;
    }

    /**
     * Converter
     *
     * @param n
     * @return
     */
    public static byte[] toBytes(long n) {
        byte[] r = new byte[8];
        for (int i = 0; i < 7; i++) {
            r[i] = (byte) (n % 256);
            n /= 256;
        }
        r[7] = (byte) n;
        return r;
    }
}

Related

  1. toBytes(int integer)
  2. toBytes(int integer)
  3. toBytes(int intValue)
  4. toBytes(int is)
  5. toBytes(int megabytes)
  6. toBytes(int n)
  7. toBytes(int val)
  8. toBytes(int value)
  9. toBytes(int value)