Skip to content

Instantly share code, notes, and snippets.

@rkdgusrnrlrl
Created April 28, 2016 14:18
Show Gist options
  • Save rkdgusrnrlrl/9b8a3de6c04940144c6e118e48597dd8 to your computer and use it in GitHub Desktop.
Save rkdgusrnrlrl/9b8a3de6c04940144c6e118e48597dd8 to your computer and use it in GitHub Desktop.
class ToInt{
//InputStream 구현시 read() 메서드는 byte 값이 아닌 int 값을 반환 하는데
//Stream 이 끝일 경우 -1을 반환하는 규약이 있어 음수를 사용 하지 못해
//byte 보다 큰 int 로 리턴하는 것이고 0 ~ 255(128 + 127) 까지 의 숫자를 리턴한다
public static int toInt(byte b) {
return (-(b-127)/128)*256+b;
}
public static void main(String[] args){
byte b = -128;
System.out.println(toInt(b));
b = 0;
System.out.println(toInt(b));
b = -127;
System.out.println(toInt(b));
b = 127;
System.out.println(toInt(b));
b = -1;
System.out.println(toInt(b));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment