Here you can find the source of numToBytes(long value, byte[] b, int off, int len)
Writes a signed integer into a byte array.
Parameter | Description |
---|---|
b | Byte array to write to. |
off | Offset within <code>b</code> of MSB of integer. |
len | Length of integer in octets. |
public static void numToBytes(long value, byte[] b, int off, int len)
//package com.java2s; /*//from w ww . ja v a 2 s . c o m * Distributed bus system for robotic applications * Copyright (C) 2009 University of Cambridge * * 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 2 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, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * USA */ public class Main { /** <p>Writes a signed integer into a byte array. Writes an * integer of <code>len*8</code> bytes into the byte array * <code>b</code>, with the MSB at <code>b[off]<code> and the LSB * at <code>b[off+len-1<code>. The data is encoded in two's * complement format, and in network byte order.</p> * * @param b Byte array to write to. * @param off Offset within <code>b</code> of MSB of integer. * @param len Length of integer in octets. */ public static void numToBytes(long value, byte[] b, int off, int len) { /* Shift in the data */ long shift = value; for (int i = len - 1; i >= 0; i--) { long v = shift & 0xff; if ((v & 0x80) > 0) { v -= 0x100; } b[off + i] = (byte) v; shift = shift >> 8; } } }