/Users/lyon/j4p/src/utils/UByte.java

1    package utils; 
2     
3    // and unsigned 8 bit byte in java 
4     
5    public final class UByte { 
6        private UByte() { 
7        }; 
8     
9        // strip sign, unsigned int 
10       public static int ui(byte b) { 
11           if (b < 0) { 
12               return 256 + b; 
13           } 
14           return b; 
15       } 
16    
17       // unsigned short 
18       public static short us(byte b) { 
19           if (b < 0) { 
20               return (short) (256 + b); 
21           } 
22           return (short) b; 
23       } 
24    
25       public static void main(String args[]) { 
26           byte b = -2; 
27           System.out.println("Ubyte.ss(" + b + ")=" + UByte.us(b)); 
28           b = 127; 
29           System.out.println("Ubyte.ss(" + b + ")=" + UByte.us(b)); 
30           b = (byte) 128; 
31           System.out.println("Ubyte.ss(" + b + ")=" + UByte.us(b)); 
32    
33       } 
34   }