Convert the specified short into a byte array. - Java File Path IO

Java examples for File Path IO:Byte Array

Description

Convert the specified short into a byte array.

Demo Code

/*//w w w.j  a  va2s .c om
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
//package com.java2s;

public class Main {
    /**
     * Convert the specified short into a byte array.
     *
     * @param value
     *            The value to be converted.
     * @param array
     *            The array to receive the data.
     * @param offset
     *            The offset into the byte array for the start of the value.
     */
    public static void shortToByteArray(final short value,
            final byte[] array, final int offset) {
        array[offset] = (byte) (value >>> 8);
        array[offset + 1] = (byte) value;
    }

    /**
     * Convert the specified short into a byte array.
     *
     * @param value
     *            The value to be converted.
     * @return The byte array
     */
    public static byte[] shortToByteArray(final short value) {
        final byte[] serverValue = new byte[2];
        shortToByteArray(value, serverValue, 0);
        return serverValue;
    }
}

Related Tutorials