Java Endian Swap swapEndian2Bytes(short sEndian)

Here you can find the source of swapEndian2Bytes(short sEndian)

Description

Swap the endian-ness of two bytes.

License

Apache License

Parameter

Parameter Description
sEndian the short containing the two bytes

Return

a short containing the two bytes with the endian-ness swapped

Declaration

public static byte[] swapEndian2Bytes(short sEndian) 

Method Source Code

//package com.java2s;
/*//w w  w .j a  va 2  s . c o m
 * Copyright 2017 Marcus Portmann
 *
 * Licensed 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.
 */

public class Main {
    /**
     * Swap the endian-ness of two bytes.
     *
     * @param sEndian the short containing the two bytes
     *
     * @return a short containing the two bytes with the endian-ness swapped
     */
    public static byte[] swapEndian2Bytes(short sEndian) {
        byte[] b = new byte[2];

        b[0] = (byte) (sEndian >>> 8);
        b[1] = (byte) sEndian; /* cast implies & 0xff */

        return b;
    }
}

Related

  1. swapEndian(final int i)
  2. swapEndianHexString(String in)
  3. swapEndianness(int i)