Here you can find the source of toBytes(int n)
Parameter | Description |
---|---|
n | a parameter |
public static byte[] toBytes(int n)
//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; } }