Combine 2 bytes (high byte and low byte) to one whole byte - Java java.lang

Java examples for java.lang:byte Array

Description

Combine 2 bytes (high byte and low byte) to one whole byte

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        byte high = 2;
        byte low = 2;
        System.out.println(combine2bytesToOne(high, low));
    }/*  ww  w . j a  v a2  s  .  com*/

    /**
     * Combine 2 bytes (high byte and low byte) to one whole byte
     * 
     * @param high
     *            the high byte
     * @param low
     *            the low byte
     * @return the whole byte
     */
    public static byte combine2bytesToOne(byte high, byte low) {
        if (high < 0 || high > 0xf || low < 0 || low > 0xf) {
            throw new RuntimeException("Out of Boundary");
        }
        return (byte) (high << 4 | low);
    }
}

Related Tutorials