ConvertTest

1225 단어
import java.util.*;
class ConvertTest 
{
		/**
	  *          int
	  * @param b byte[]
	  * @return int
	  */
	public static int hBytesToInt(byte[] b) {
	  int s = 0;
	  for (int i = 0; i < 3; i++) {
		if (b[i] >= 0) {
		s = s + b[i];
		} else {
		s = s + 256 + b[i];
		}
		s = s * 256;
	  }
	  if (b[3] >= 0) {
		s = s + b[3];
	  } else {
		s = s + 256 + b[3];
	  }
	  return s;
	}



	public static void main(String[] args) 
	{
		String str="123";
		int i=Integer.parseInt(str);
		System.out.println("i= "+i);

		byte[] btarry=str.getBytes();
		System.out.println("btarry=" + Arrays.toString(btarry));

		String str2 = new String(btarry);
		System.out.println("str2= "+str2);

		int j=Integer.parseInt(str2);
		System.out.println("j= "+j);


		

		System.out.println("Hello World!");//System.out.println((char)(0x30));//0
		System.out.println(hBytesToInt(btarry));
	}
}

/*
i= 123
btarry=[49, 50, 51]
str2= 123
j= 123
Hello World!
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
        at ConvertTest.hBytesToInt(ConvertTest.java:19)
        at ConvertTest.main(ConvertTest.java:48)
       . . .
*/

좋은 웹페이지 즐겨찾기