태터데스크 관리자

도움말
닫기
적용하기   첫페이지 만들기

태터데스크 메시지

저장하였습니다.

Java 에서 LittleEndian 과 BigEndian

Posted 2008/09/19 10:42


댓글 하나가 운영자에겐 커다란 힘이 됩니다!

BigEndian 과 LittleEndian

서로 다른 서버와 서로 다른 Language 에서 네트워크 통신을 하다보면

종종 Byte Ordering 때문에 곤욕을 치르곤 한다.

오래전에 BigEndian 과 LittleEndian 을 찾아보면 고생했던 생각이 새록새록 떠오른다.ㅡㅡ;;

두개의 Endian에 대해서는 검색을 해보면 많은 얘기가 나오기 때문에 Endian 에 대해서는 별도로 설명하지 않을란다.^^;;

암튼 네트워크 통신을 할때 가장 중요한 부분이 바로 Byte Ordering 이다.

반드시 서로 연동해야할 시스템이 있다면 먼저 이 Byte Ordering 이 Network Order 인지 아닌지 구분을 하고

그 이후에 연동을 하기 바란다.

아래는 소스 코드이다.

소스를 복사해서 쓰기 전에 먼저 Endian 에 대해서 열심히 알아보고 사용하기를 바란다.

 

/**************************************************************************

 LittleEndian Source : LittleEndianByteHandler.java

 *************************************************************************/

public class LittleEndianByteHandler{
 
 /**
  * short type을 byte로 변형
  **/
 public static final byte[] shortToByte(short shortVar){
  byte littleShort[] = new byte[2];
  littleShort[0] = (byte)((shortVar>>0) & 0xff);
  littleShort[1] = (byte)((shortVar>>8) & 0xff);
  return littleShort;
 }

 /**
  * int type을 byte로 변형
  **/
 public static final byte[] intToByte(int intVar){
  byte littleInt[] = new byte[4];
  littleInt[0] = (byte)((intVar>>0) & 0xff);
  littleInt[1] = (byte)((intVar>>8) & 0xff);
  littleInt[2] = (byte)((intVar>>16) & 0xff);
  littleInt[3] = (byte)((intVar>>24) & 0xff);
  return littleInt;
 }

 /**
  * int type을 byte로 변형
  **/
 public static final byte[] intToByte2(int intVar){
  byte littleInt[] = new byte[2];
  littleInt[0] = (byte)((intVar>>0) & 0xff);
  littleInt[1] = (byte)((intVar>>8) & 0xff);
  return littleInt;
 }
 
 /**
  * long type을 byte로 변형
  **/  
 public static final byte[] longToByte(long longVar){
  byte littleLong[] = new byte[8];  
  littleLong[0] = (byte)((longVar>>0) & 0xff);
  littleLong[1] = (byte)((longVar>>8) & 0xff);
  littleLong[2] = (byte)((longVar>>16) & 0xff);
  littleLong[3] = (byte)((longVar>>24) & 0xff);
  littleLong[4] = (byte)((longVar>>32) & 0xff);
  littleLong[5] = (byte)((longVar>>40) & 0xff);
  littleLong[6] = (byte)((longVar>>48) & 0xff);
  littleLong[7] = (byte)((longVar>>56) & 0xff);      
  return littleLong;
 }
 
 /**
  * double type을 byte로 변형
  **/  
 public static final byte[] doubleToByte(double doubleVar){
  long temp;  
  byte littleDouble[] = new byte[8];
  temp = Double.doubleToLongBits(doubleVar);
  littleDouble = longToByte(temp);  
  return littleDouble;
 }

 /**
  * byte 배열을 short 형으로 변환한다.
  **/
 public static final short byteToShort(byte[] buffer){
  return byteToShort(buffer, 0);
 }
 public static final short byteToShort(byte[] buffer, int offset){  
  return (short) ( (buffer[offset+1]&0xff)<<8 | (buffer[offset]&0xff) );
 }


 /**
  * byte 배열을 int 형으로 변환한다.
  **/
 public static final int byte1ToInt(byte b){
  return (int)(b&0xff);
 } 
 public static final int byte1ToInt(byte[] b, int offset){
  return (int)(b[offset]&0xff);
 }
 public static final int byte2ToInt(byte[] buffer, int offset){
  return (buffer[offset+1]&0xff)<<8 | (buffer[offset]&0xff);
 }
 public static final int byteToInt(byte[] buffer){
  return byteToInt(buffer, 0);
 }
 public static final int byteToInt(byte[] buffer, int offset){  
  return (buffer[offset+3]&0xff)<<24 | (buffer[offset+2]&0xff)<<16 | (buffer[offset+1]&0xff)<<8 | (buffer[offset]&0xff);
 }

 /**
  * byte 배열을 long 형으로 변환한다.
  **/
 public static final long byteToLong(byte[] buffer){  
  return byteToLong(buffer, 0);
 }
 
 public static final long byteToLong(byte[] buffer, int offset){
  return  ((long)byteToInt(buffer, offset+4) <<32) | ((long) byteToInt(buffer,offset)  & 0xffffffffL );
 }
   
    /**
  * byte 배열을 double 형으로 변환한다.
  **/
 public static final double byteToDouble(byte[] buffer, int offset){
  long temp = byteToLong(buffer, offset);
  return Double.longBitsToDouble(temp);
 } 
 public static final double byteToDouble(byte[] buffer){
  long temp = byteToLong(buffer,0);
  return Double.longBitsToDouble(temp);
 }
}

 

/**************************************************************************

 BigEndian Source : BigEndianByteHandler.java

 *************************************************************************/

public class BigEndianByteHandler {   

 /**
  * short type을 byte로 변형
  **/
    public static final byte[] shortToByte( short s ) {
        byte[] dest = new byte[2];
        dest[1] = (byte)(s & 0xff);
        dest[0] = (byte)((s>>8) & 0xff);
        return dest;
    }
   /**
  * int type을 byte로 변형
  **/
    public static final byte[] intToByte( int i ) {
        byte[] dest = new byte[4];
        dest[3] = (byte)(i & 0xff);
        dest[2] = (byte)((i>>8) & 0xff);
        dest[1] = (byte)((i>>16) & 0xff);
        dest[0] = (byte)((i>>24) & 0xff);
        return dest;
    }
 /**
  * int type을 byte로 변형
  **/
    public static final byte[] intToByte2( int i ) {
        byte[] dest = new byte[2];
  dest[1] = (byte)(i & 0xff);
        dest[0] = (byte)((i>>8) & 0xff);
  return dest;
    }
   /**
 * long type을 byte로 변형
 **/
    public static final byte[] longToByte( long l ) {
        byte[] dest = new byte[8];
        dest[7] = (byte)(l & 0xff);
        dest[6] = (byte)((l>>8) & 0xff);
        dest[5] = (byte)((l>>16) & 0xff);
        dest[4] = (byte)((l>>24) & 0xff);
        dest[3] = (byte)((l>>32) & 0xff);
        dest[2] = (byte)((l>>40) & 0xff);
        dest[1] = (byte)((l>>48) & 0xff);
        dest[0] = (byte)((l>>56) & 0xff);
        return dest;
    } 
 
  /**
  * double type을 byte로 변형
  **/  
 public static final byte[] doubleToByte(double d) {
  long temp;  
  byte dest[] = new byte[8];
  temp = Double.doubleToLongBits(d);
  dest = longToByte(temp);  
  return dest;
 }

   
 /**
  * byte 배열을 short 형으로 변환한다.
  **/
    public static final short byteToShort( byte[] src, int offset ) {
        return (short)( ((src[offset]&0xff) << 8) | (src[offset+1]&0xff) ) ;
    }
 public static final short byteToShort( byte[] src){
  return byteToShort( src, 0 );
 }
  
    /**
  * byte 배열을 int 형으로 변환한다.
  **/
    public static final int byteToInt( byte[] src, int offset ) {
        return ((src[offset]&0xff) << 24) | ((src[offset+1]&0xff) << 16) |
                  ((src[offset+2]&0xff) << 8) | (src[offset+3]&0xff) ;
    }
    public static final int byte2ToInt( byte[] src, int offset ) {
        return ((src[offset]&0xff) << 8) | (src[offset+1]&0xff) ;
    }
    public static final int byteToInt( byte[] src, int offset, int len ) {
     int intValue = 0;
     for(int i=0; i<len; i++)
      intValue |= (src[offset + i]&0xff) << 8*(3 - i);
      
     return intValue;
    }
 public static final int byteToInt(byte[] src){
  return byteToInt( src, 0 );
 }
    /**
  * byte 배열을 long 형으로 변환한다.
  **/   
    public static final long byteToLong( byte[] src, int offset ) {
        return ((long) byteToInt(src,offset) << 32 ) |
                  ((long) byteToInt(src, offset+4) & 0xffffffffL ) ; 
    }
 public static final long byteToLong(byte[] src){
  return byteToLong( src, 0 );
 }

    /**
  * byte 배열을 double 형으로 변환한다.
  **/   
  public static final double byteToDouble(byte[] buffer, int offset){
  long temp = byteToLong(buffer, offset);
  return Double.longBitsToDouble(temp);
 }
 public static final double byteToDouble(byte[] src){
  return byteToDouble( src, 0 );
 }
   
    /**
     * byte 배열에 short 값을 배정
     **/
    public static final byte[] setShort( byte[] dest, int offset, short s ) {
        dest[offset+1] = (byte)(s & 0xff);
        dest[offset] = (byte)((s>>8) & 0xff);
        return dest;
    }   
   
    /**
     * byte 배열에 Int 값을 배정
     **/
    public static final byte[] setInt( byte[] dest, int offset, int i ) {
        dest[offset+3] = (byte)(i & 0xff);
        dest[offset+2] = (byte)((i>>8) & 0xff);
        dest[offset+1] = (byte)((i>>16) & 0xff);
        dest[offset] = (byte)((i>>24) & 0xff);
        return dest;
    }   
   
    /**
     * byte 배열에 Long 값을 배정
     **/
    public static final byte[] setLong( byte[] dest, int offset, long l ) {
        setInt(dest, offset, (int)(l>>32) );
        setInt(dest, offset + 4, (int)(l & 0xffffffffL));
        return dest;
    }   
}

위의 정보가 도움이 되셨나요? 그렇다면 댓글 하나만 남겨주세요.
댓글 하나가 운영자에겐 커다란 힘이 됩니다!

Write your message and submit
« PREV : 1 : ... 179 : 180 : 181 : 182 : 183 : 184 : 185 : 186 : 187 : ... 436 : NEXT »