Wednesday, January 9, 2008

Unsigned Primitive Type in Java

1- You can use Javalution.org packages to use unsigned types.

2- Converting unsigned types to standard java types:
byte b;
int i = b & 0xff;

int i;
long l = i & 0xffffffffL;

3- Combining two unsigned shorts into an unsigned int.
short ush = 4;
short
usl = 9999;
int
ucombined = ( ush & 0xffff ) << 16 | ( usl & 0xffff );

4-/**
* Convert an unsigned int to a String.
* @param i the int to convert.
* @return the equivalent unsigned String
*/
public static void unsignedToString( int i )
{
return Long.toString( i & 0xffffffffL )
}

No comments: