ucar.unidata.io
Class Swap

java.lang.Object
  extended by ucar.unidata.io.Swap

public class Swap
extends Object

The Swap class provides static methods for swapping the bytes of chars, shorts, ints, longs, floats, and doubles.

Version:
$Id: Swap.java 64 2006-07-12 22:30:50Z edavis $
Author:
Kirk Waters, Russ Rew, 1998, added documentation

Constructor Summary
Swap()
           
 
Method Summary
static byte[] intToBytes(int v)
          Convert an int to an array of 4 bytes.
static byte[] longToBytes(long v)
          Convert a long to an array of 8 bytes.
static byte[] shortToBytes(short v)
          Convert a short to an array of 2 bytes.
static char swapChar(byte[] b, int offset)
          Returns the char resulting from swapping 2 bytes at a specified offset in a byte array.
static double swapDouble(byte[] b, int offset)
          Returns the double resulting from reversing 8 bytes at a specified offset in a byte array.
static double swapDouble(double v)
          Returns the double resulting from reversing 8 bytes of a specified double.
static float swapFloat(byte[] b, int offset)
          Returns the float resulting from reversing 4 bytes at a specified offset in a byte array.
static float swapFloat(float v)
          Returns the float resulting from reversing 4 bytes of a specified float.
static int swapInt(byte[] b, int offset)
          Returns the int resulting from reversing 4 bytes at a specified offset in a byte array.
static int swapInt(int v)
          Returns the int resulting from reversing 4 bytes of a specified int.
static long swapLong(byte[] b, int offset)
          Returns the long resulting from reversing 8 bytes at a specified offset in a byte array.
static long swapLong(long l)
          Returns the long resulting from reversing 8 bytes of a specified long.
static short swapShort(byte[] b, int offset)
          Returns the short resulting from swapping 2 bytes at a specified offset in a byte array.
static short swapShort(short s)
          Returns the short resulting from swapping 2 bytes of a specified short.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Swap

public Swap()
Method Detail

swapShort

public static short swapShort(byte[] b,
                              int offset)
Returns the short resulting from swapping 2 bytes at a specified offset in a byte array.

Parameters:
b - the byte array
offset - the offset of the first byte
Returns:
the short represented by the bytes b[offset+1], b[offset]

swapInt

public static int swapInt(byte[] b,
                          int offset)
Returns the int resulting from reversing 4 bytes at a specified offset in a byte array.

Parameters:
b - the byte array
offset - the offset of the first byte
Returns:
the int represented by the bytes b[offset+3], b[offset+2], ..., b[offset]

swapLong

public static long swapLong(byte[] b,
                            int offset)
Returns the long resulting from reversing 8 bytes at a specified offset in a byte array.

Parameters:
b - the byte array
offset - the offset of the first byte
Returns:
the long represented by the bytes b[offset+7], b[offset+6], ..., b[offset]

swapFloat

public static float swapFloat(byte[] b,
                              int offset)
Returns the float resulting from reversing 4 bytes at a specified offset in a byte array.

Parameters:
b - the byte array
offset - the offset of the first byte
Returns:
the float represented by the bytes b[offset+3], b[offset+2], ..., b[offset]

swapDouble

public static double swapDouble(byte[] b,
                                int offset)
Returns the double resulting from reversing 8 bytes at a specified offset in a byte array.

Parameters:
b - the byte array
offset - the offset of the first byte
Returns:
the double represented by the bytes b[offset+7], b[offset+6], ..., b[offset]

swapChar

public static char swapChar(byte[] b,
                            int offset)
Returns the char resulting from swapping 2 bytes at a specified offset in a byte array.

Parameters:
b - the byte array
offset - the offset of the first byte
Returns:
the char represented by the bytes b[offset+1], b[offset]

swapShort

public static short swapShort(short s)
Returns the short resulting from swapping 2 bytes of a specified short.

Parameters:
s - input value for which byte reversal is desired
Returns:
the value represented by the bytes of s reversed

swapInt

public static int swapInt(int v)
Returns the int resulting from reversing 4 bytes of a specified int.

Parameters:
v - input value for which byte reversal is desired
Returns:
the value represented by the bytes of v reversed

swapLong

public static long swapLong(long l)
Returns the long resulting from reversing 8 bytes of a specified long.

Parameters:
l - input value for which byte reversal is desired
Returns:
the value represented by the bytes of l reversed

swapFloat

public static float swapFloat(float v)
Returns the float resulting from reversing 4 bytes of a specified float.

Parameters:
v - input value for which byte reversal is desired
Returns:
the value represented by the bytes of v reversed

swapDouble

public static double swapDouble(double v)
Returns the double resulting from reversing 8 bytes of a specified double.

Parameters:
v - input value for which byte reversal is desired
Returns:
the value represented by the bytes of v reversed

shortToBytes

public static byte[] shortToBytes(short v)
Convert a short to an array of 2 bytes.

Parameters:
v - input value
Returns:
the corresponding array of bytes

intToBytes

public static byte[] intToBytes(int v)
Convert an int to an array of 4 bytes.

Parameters:
v - input value
Returns:
the corresponding array of bytes

longToBytes

public static byte[] longToBytes(long v)
Convert a long to an array of 8 bytes.

Parameters:
v - input value
Returns:
the corresponding array of bytes


Copyright © 1999-2011 UCAR/Unidata. All Rights Reserved.