set Value To Byte Array Little Endian - Java Internationalization

Java examples for Internationalization:Charset

Description

set Value To Byte Array Little Endian

Demo Code


//package com.java2s;

public class Main {
    public static void setValueToByteArrayLittleEndian(short val,
            byte[] byteArray, int offset) {
        byteArray[offset] = (byte) (val & 0x00FF);
        byteArray[offset + 1] = (byte) ((val >> 8) & 0x00FF);
    }/*from  w w  w. j a  v a2  s .  com*/

    public static void setValueToByteArrayLittleEndian(int val,
            byte[] byteArray, int offset) {
        byteArray[offset] = (byte) (val & 0xFF);
        byteArray[offset + 1] = (byte) ((val >> 8) & 0xFF);
        byteArray[offset + 2] = (byte) ((val >> 16) & 0xFF);
        byteArray[offset + 3] = (byte) ((val >> 24) & 0xFF);
    }

    public static void setValueToByteArrayLittleEndian(long val,
            byte[] byteArray, int offset) {
        byteArray[offset] = (byte) (val & 0xFFL);
        byteArray[offset + 1] = (byte) ((val >> 8) & 0xFFL);
        byteArray[offset + 2] = (byte) ((val >> 16) & 0xFFL);
        byteArray[offset + 3] = (byte) ((val >> 24) & 0xFFL);
        byteArray[offset + 4] = (byte) ((val >> 32) & 0xFFL);
        byteArray[offset + 5] = (byte) ((val >> 40) & 0xFFL);
        byteArray[offset + 6] = (byte) ((val >> 48) & 0xFFL);
        byteArray[offset + 7] = (byte) ((val >> 56) & 0xFFL);
    }
}

Related Tutorials