Java Endian Swap swapEndian(final int i)

Here you can find the source of swapEndian(final int i)

Description

Converts an int from little endian to big endian, or vice versa.

License

Open Source License

Parameter

Parameter Description
i an <code>int</code> value

Return

an int value, with the endianness swapped.

Declaration

public static int swapEndian(final int i) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from   w  ww.  j a va 2  s  . c om*/
     * Converts an int from little endian to big endian, or vice versa.
     * @param i an <code>int</code> value
     * @return an <code>int</code> value, with the endianness swapped.
     */
    public static int swapEndian(final int i) {
        return ((i & 0xff) << 24) | ((i & 0xff00) << 8) | ((i >>> 8) & 0xff00) | ((i >>> 24) & 0xff);
    }

    /**
     * Converts an long from little endian to big endian, or vice versa.
     * @param l a <code>long</code> value
     * @return a <code>long</code> value, with the endianness swapped.
     */
    public static long swapEndian(final long l) {
        return ((l & 0xff) << 56) | ((l & 0xff00) << 40) | ((l & 0xff0000) << 24) | ((l & 0xff000000L) << 8)
                | ((l >>> 8) & 0xff000000L) | ((l >>> 24) & 0xff0000) | ((l >>> 40) & 0xff00) | ((l >>> 56) & 0xff);
    }
}

Related

  1. swapEndian2Bytes(short sEndian)
  2. swapEndianHexString(String in)
  3. swapEndianness(int i)