2010-04-14 4 views
0

Y at-il quelque chose comme ça dans JDK ou Apache Commons (ou dans un autre pot)?octet toPositiveInt méthode

/** 
* Return the integer positive value of the byte. (e.g. -128 will return 
* 128; -127 will return 129; -126 will return 130...) 
*/ 
public static int toPositiveInt(byte b) { 
int intV = b; 
if (intV < 0) { 
    intV = -intV; 
    int diff = ((Byte.MAX_VALUE + 1) - intV) + 1; 
    intV = Byte.MAX_VALUE + diff; 
} 
return intV; 
    } 

Répondre

3

Habituellement, vous utilisez une manipulation de bits de base pour ceci:

public static int toPositiveInt(byte b) { 
return b & 0xFF; 
} 

Et parce qu'il est si courte, il est généralement inline et non appelé comme méthode.

+0

cool ........ XD –

+0

J'avais complètement oublié la manipulation des bits existe –