com.javaranch.common
Class Numbers

java.lang.Object
  |
  +--com.javaranch.common.Numbers

public class Numbers
extends java.lang.Object

Contains a collection of static methods related to Numbers.

- - - - - - - - - - - - - - - - -

Copyright (c) 1998-2004 Paul Wheaton

You are welcome to do whatever you want to with this source file provided that you maintain this comment fragment (between the dashed lines). Modify it, change the package name, change the class name ... personal or business use ... sell it, share it ... add a copyright for the portions you add ...

My goal in giving this away and maintaining the copyright is to hopefully direct developers back to JavaRanch.

The original source can be found at JavaRanch

- - - - - - - - - - - - - - - - -

Author:
Paul Wheaton

, jos hirth


Constructor Summary
Numbers()
           
 
Method Summary
static int byteToPositiveInt(byte b)
          Convert a possibly negative valued byte to a positive value integer.
static double decimalDegrees(java.lang.String text)
          Pass in a string that represents some form of lat or long and a decimal value is returned.
static int endian(int x)
          Reverse the byte order of a four byte integer.
static short endian(short x)
          Swap the upper and lower bytes of a two byte integer.
static boolean inRange(char x, char min, char max)
          A convenience method to test if a character is within a particular range.
static boolean inRange(double x, double min, double max)
          A convenience method to test if a double is within a particular range.
static boolean inRange(int x, int min, int max)
          A convenience method to test if an integer is within a particular range.
static byte positiveIntToByte(int i)
          Convert an integer possibly negative valued byte to a positive value integer.
static int[] toIntArray(java.util.Set values)
          Each value will be cast to Integer and the values will be stored in an array.
static java.util.Set toSet(int[] values)
          Each int in the array is encapsulated in an Integer object and added to the set.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Numbers

public Numbers()
Method Detail

endian

public static short endian(short x)
Swap the upper and lower bytes of a two byte integer.

This is useful when you need to convert between Intel formatted integers(little endian) and Java/Motorola formatted integers(big endian).

Parameters:
x - The number to be converted.

Returns:
The converted number.


endian

public static int endian(int x)
Reverse the byte order of a four byte integer.

This is useful when you need to convert between Intel formatted integers(little endian) and Java/Motorola formatted integers(big endian).

If the original hex number was ABCD, the new hex number will be DCBA.

Parameters:
x - The number to be converted.

Returns:
The converted number.


byteToPositiveInt

public static int byteToPositiveInt(byte b)
Convert a possibly negative valued byte to a positive value integer.

A Java byte has a value of -128 to 127. With eight bits and no sign, the negative numbers could be represented as a value of 128 to 255. This method makes such a conversion, but stores the result in an integer since Java does not support unsigned bytes.

Parameters:
b - The byte with a possibly negative value.

Returns:
An integer with a value of 0 to 255.


positiveIntToByte

public static byte positiveIntToByte(int i)
Convert an integer possibly negative valued byte to a positive value integer.

A Java byte has a value of -128 to 127. With eight bits and no sign, the negative numbers could be represented as a value of 128 to 255. This method makes the conversion from 0-255 to -128-127.

This method assumes your integer will be of the range of 0 to 255. Using a value outside of this range could generate an exception.

Parameters:
i - The integer in the range of 0 to 255.

Returns:
A byte with a possibly negative value.


inRange

public static boolean inRange(int x,
                              int min,
                              int max)
A convenience method to test if an integer is within a particular range.

Parameters:
x - The value to test.

min - The minimum value for x.

max - The maximum value for x.

Returns:
true if ( x >= min ) and ( x <= max ).


inRange

public static boolean inRange(double x,
                              double min,
                              double max)
A convenience method to test if a double is within a particular range.

Parameters:
x - The value to test.

min - The minimum value for x.

max - The maximum value for x.

Returns:
true if ( x >= min ) and ( x <= max ).


inRange

public static boolean inRange(char x,
                              char min,
                              char max)
A convenience method to test if a character is within a particular range.

Parameters:
x - The character to test.

min - The minimum value for x.

max - The maximum value for x.

Returns:
true if ( x >= min ) and ( x <= max ).


decimalDegrees

public static double decimalDegrees(java.lang.String text)
Pass in a string that represents some form of lat or long and a decimal value is returned.


toSet

public static java.util.Set toSet(int[] values)
Each int in the array is encapsulated in an Integer object and added to the set.


toIntArray

public static int[] toIntArray(java.util.Set values)
Each value will be cast to Integer and the values will be stored in an array. This will throw a casting exception if the objects are not all Integer!



Copyright ©2004 Paul Wheaton All Rights Reserved