Java Byte Array Create toBytes(final long val)

Here you can find the source of toBytes(final long val)

Description

Converts a 32 bit value to an array of bytes.

License

Open Source License

Parameter

Parameter Description
val The 32-bit value to convert

Return

an array containing 4 bytes extracted from the value

Declaration

public static byte[] toBytes(final long val) 

Method Source Code

//package com.java2s;
/* Penny Post - A postage system for email
 * Copyright (c) 2006-2007  Gregory Rubin <grrubin@gmail.com> 
 * http://www.nettgryppa.com /*from w  ww  . j  a va  2  s. c om*/
*  Copyright (C) 2007  Aliasgar Lokhandwala <d7@freepgs.com> 
 * http://pennypost.sourceforge.net/
 * 
 * CashUtils.java: Provides common helper methods
 *
 * 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 {
    /**
     * Converts a 32 bit value to an array of bytes. Reverse of
     * unsignedIntToLong
     * 
     * @param val
     *            The 32-bit value to convert
     * @return an array containing 4 bytes extracted from the value
     * 
     * @author Ali
     * @see #unsignedIntToLong(byte[])
     */
    public static byte[] toBytes(final long val) {
        byte[] bytes = new byte[4];
        bytes[3] = (byte) (val & 0xFF);
        bytes[2] = (byte) ((val & 0xFF00) >>> 8);
        bytes[1] = (byte) ((val & 0xFF0000) >>> 16);
        bytes[0] = (byte) ((val & 0xFF000000) >>> 32);
        return bytes;
    }
}

Related

  1. toBytes(char value)
  2. toBytes(char[] cbuf, byte[] bs)
  3. toBytes(char[] chars)
  4. toBytes(final byte[] s, final int off, final int len)
  5. toBytes(final long n)
  6. toBytes(final String hexaString)
  7. toBytes(final String str)
  8. toBytes(final String text)
  9. toBytes(int data)