본문 바로가기

Java

각 자료형<->byte

이거하느라 노가다좀 했음..... 


boolean은 왜 없냐고 물어본다면 자바 자료형부터 다시 공부하길....


boolean은 논리형이기 때문에 변경할수가 없다.



byte를 자료형으로......


public int byteToInt(byte[] data) {

int result = 0;

result += ((data[0] & 0xff) << (24));

result += ((data[1] & 0xff) << (16));

result += ((data[2] & 0xff) << (8));

result += ((data[3] & 0xff));


return result;

}


public int byteToInt(byte[] data, int offset) {

int result = 0;

result += ((data[0 + offset] & 0xff) << (24));

result += ((data[1 + offset] & 0xff) << (16));

result += ((data[2 + offset] & 0xff) << (8));

result += ((data[3 + offset] & 0xff));


return result;

}


public float byteToFloat(byte[] data) {

int result = 0;

result += ((data[0] & 0xff) << (24));

result += ((data[1] & 0xff) << (16));

result += ((data[2] & 0xff) << (8));

result += ((data[3] & 0xff));

return Float.intBitsToFloat(result);


}


public float byteToFloat(byte[] data, int offset) {

int result = 0;

result += ((data[0 + offset] & 0xff) << (24));

result += ((data[1 + offset] & 0xff) << (16));

result += ((data[2 + offset] & 0xff) << (8));

result += ((data[3 + offset] & 0xff));

return Float.intBitsToFloat(result);


}


public static long byteToLong(byte[] data) {

long result = 0;

result += ((long) (0xff & data[0]) << (56));

result += ((long) (0xff & data[1]) << (48));

result += ((long) (0xff & data[2]) << (40));

result += ((long) (0xff & data[3]) << (32));

result += ((long) (0xff & data[4]) << (24));

result += ((long) (0xff & data[5]) << (16));

result += ((long) (0xff & data[6]) << (8));

result += ((long) (0xff & data[7]));


return result;


}


public long byteToLong(byte[] data, int offset) {

long result = 0;

result += ((long) (0xff & data[0 + offset]) << (56));

result += ((long) (0xff & data[1 + offset]) << (48));

result += ((long) (0xff & data[2 + offset]) << (40));

result += ((long) (0xff & data[3 + offset]) << (32));

result += ((long) (0xff & data[4 + offset]) << (24));

result += ((long) (0xff & data[5 + offset]) << (16));

result += ((long) (0xff & data[6 + offset]) << (8));

result += ((long) (0xff & data[7 + offset]));


return result;

}


public short byteToShort(byte[] data) {

short result = 0;

result += ((data[0] & 0xff) << (8));

result += ((data[1] & 0xff));

return result;

}


public short byteToShort(byte[] data, int offset) {

short result = 0;

result += ((data[0 + offset] & 0xff) << (8));

result += ((data[1 + offset] & 0xff));

return result;

}


public byte byteToByte(byte[] data) {

byte result = 0;

result += ((data[0] & 0xff));


return result;

}


public byte byteToByte(byte[] data, int offset) {

byte result = 0;

result += ((data[0 + offset] & 0xff));


return result;

}


public char byteToChar(byte[] data) {

char result = 0;

result += ((data[0] & 0xff) << (8));

result += ((data[1] & 0xff));


return result;

}


public char byteToChar(byte[] data, int offset) {

char result = 0;

result += ((data[0 + offset] & 0xff) << (8));

result += ((data[1 + offset] & 0xff));


return result;

}


public double byteToDouble(byte[] data) {

long result = 0;

result += ((long) (0xff & data[0]) << (56));

result += ((long) (0xff & data[1]) << (48));

result += ((long) (0xff & data[2]) << (40));

result += ((long) (0xff & data[3]) << (32));

result += ((long) (0xff & data[4]) << (24));

result += ((long) (0xff & data[5]) << (16));

result += ((long) (0xff & data[6]) << (8));

result += ((long) (0xff & data[7]));

return Double.longBitsToDouble(result);


}


public double byteToDouble(byte[] data, int offset) {

long result = 0;

result += ((long) (0xff & data[0 + offset]) << (56));

result += ((long) (0xff & data[1 + offset]) << (48));

result += ((long) (0xff & data[2 + offset]) << (40));

result += ((long) (0xff & data[3 + offset]) << (32));

result += ((long) (0xff & data[4 + offset]) << (24));

result += ((long) (0xff & data[5 + offset]) << (16));

result += ((long) (0xff & data[6 + offset]) << (8));

result += ((long) (0xff & data[7 + offset]));

return Double.longBitsToDouble(result);


}



자료형을 byte로.....



public byte[] intToByte(int a) {

byte data[] = new byte[4];

// data[0] = (byte) ((a & 0xff000000) >> 24);

// data[1] = (byte) ((a & 0x00ff0000) >> 16);

// data[2] = (byte) ((a & 0x0000ff00) >> 8);

// data[3] = (byte) (a & 0x000000ff);

data[0] = (byte) ((a >> 24) & 0xff);

data[1] = (byte) ((a >> 16) & 0xff);

data[2] = (byte) ((a >> 8) & 0xff);

data[3] = (byte) (a & 0xff);


return data;

}


public byte[] floatToByte(float a) {


int fa = Float.floatToIntBits(a);


byte data[] = new byte[4];

data[0] = (byte) ((fa & 0xff000000) >> 24);

data[1] = (byte) ((fa & 0x00ff0000) >> 16);

data[2] = (byte) ((fa & 0x0000ff00) >> 8);

data[3] = (byte) (fa & 0x000000ff);


return data;

}


public byte[] longToByte(long a) {


byte data[] = new byte[8];

data[0] = (byte) ((a >> 56) & 0xff);

data[1] = (byte) ((a >> 48) & 0xff);

data[2] = (byte) ((a >> 40) & 0xff);

data[3] = (byte) ((a >> 32) & 0xff);

data[4] = (byte) ((a >> 24) & 0xff);

data[5] = (byte) ((a >> 16) & 0xff);

data[6] = (byte) ((a >> 8) & 0xff);

data[7] = (byte) ((a) & 0xff);


return data;

}


public byte[] shortToByte(short a) {

byte data[] = new byte[2];

data[0] = (byte) ((a >> 8) & 0xff);

data[1] = (byte) ((a) & 0xff);


return data;

}


public byte[] byteToByte(byte a) {

byte data[] = new byte[1];

data[0] = (byte) ((a) & 0xff);


return data;

}


public byte[] charToByte(char a) {

byte data[] = new byte[2];

data[0] = (byte) ((a >> 8) & 0xff);

data[1] = (byte) ((a) & 0xff);


return data;

}


public byte[] doubleToByte(double a) {

long da = Double.doubleToLongBits(a);


byte data[] = new byte[8];

data[0] = (byte) ((da >> 56) & 0xff);

data[1] = (byte) ((da >> 48) & 0xff);

data[2] = (byte) ((da >> 40) & 0xff);

data[3] = (byte) ((da >> 32) & 0xff);

data[4] = (byte) ((da >> 24) & 0xff);

data[5] = (byte) ((da >> 16) & 0xff);

data[6] = (byte) ((da >> 8) & 0xff);

data[7] = (byte) ((da) & 0xff);


return data;

}



'Java' 카테고리의 다른 글

java 디자인 패턴(singleton)  (0) 2012.07.20
System.arrayCopy  (0) 2012.04.24
Buffer  (40) 2012.04.17
접근자  (0) 2012.04.06
다형성  (0) 2012.04.03