Write a short to the byte array at the given offset. - Java java.lang

Java examples for java.lang:byte Array to short

Description

Write a short to the byte array at the given offset.

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        byte[] array = new byte[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        int offset = 2;
        int v = 2;
        writeShort(array, offset, v);//from  w  ww  .j  a v  a  2  s .  c  o m
    }

    /**
     * Write a short to the byte array at the given offset.
     * 
     * @param array Array to write to
     * @param offset Offset to write to
     * @param v data
     */
    public final static void writeShort(byte[] array, int offset, int v) {
        array[offset + 0] = (byte) (v >>> 8);
        array[offset + 1] = (byte) (v >>> 0);
    }
}

Related Tutorials