Programing/JAVA

java 자바 byte 형변환 함수 정리

리커니 2016. 12. 23.
반응형

java 자바 byte 형변환 함수 정리

 

    /*bytearray to 이진수 변환*/
    public String byteArrayToBinaryString(byte[] b){
        StringBuilder sb=new StringBuilder();
        for(int i=0; i<b.length; ++i){
            sb.append(byteToBinaryString(b[i]));
        }
        return sb.toString();
    } 


     /*byte to 이진수 변환*/
    public String byteToBinaryString(byte n) {
        StringBuilder sb = new StringBuilder("00000000");
        for (int bit = 0; bit < 8; bit++) {
            if (((n >> bit) & 1) > 0) {
                sb.setCharAt(7 - bit, '1');
            }
        }
        return sb.toString();
    } 


    /* long to 8bytearray 변환*/
    public static byte[] longToBytes(long l) {
        byte[] result = new byte[8];
        for (int i = 7; i >= 0; i--) {
            result[i] = (byte)(l & 0xFF);
            l >>= 8;
        }
        return result;
    } 


    /* 8bytearray to long 변환*/
    public static long bytesToLong(byte[] b) {
        long result = 0;
        for (int i = 7; i >= 0; i--) {
            result <<= 8;
            result |= (b[i] & 0xFF);
        }
        return result;
    } 


    /*int to 4or2 bytearray 변환*/
    public static byte[] intToByteArray(int value, int lengthDiv) {
        byte[] byteArray = new byte[lengthDiv];
        if (lengthDiv == 2){
            byteArray[0] = (byte) value;
            byteArray[1] = (byte) (value >>> 8);   
        }else if (lengthDiv == 4){
            byteArray[0] = (byte)(value >> 24);
            byteArray[1] = (byte)(value >> 16);
            byteArray[2] = (byte)(value >> 8);
            byteArray[3] = (byte)(value);           
        }
        return byteArray;
    }
    
      /* 4or2 bytearray  to int 변환*/
    public static int byteArrayToInt(byte[] b, int lengthDiv){
        int byteInt = 0;
        if (lengthDiv==2){
            byteInt = ((b[1] & 0xFF) << 8) | (b[0] & 0xFF);
        }else if (lengthDiv==4){
            byteInt = b[0] & 0xFF |
                     (b[1] & 0xFF) << 8 |
                     (b[2] & 0xFF) << 16 |
                     (b[3] & 0xFF) << 24;
        }
        return byteInt;
    }

    /* shot to byte 변환 */
    public static byte[] shortToBytes(short Value, int Order){
        byte[] temp;
        temp = new byte[]{ (byte)((Value & 0xFF00) >> 8), (byte)(Value & 0x00FF) };
        temp = ChangeByteOrder(temp,Order);
        return temp;
    } 


    /* 상위, 하위 변환 (내부적으로 사용하는 함수)*/
    public static byte[] ChangeByteOrder(byte[] value,int Order){
        int idx = value.length;
        byte[] Temp = new byte[idx];
        //BIG_EDIAN
        if(Order == 1){
            Temp = value;
        }else if(Order == 0){
            for(int i=0;i<idx;i++) {
                Temp[i] = value[idx-(i+1)];
            }
        }
        return Temp;
    } 


    /* 2진수String to byte 변환*/
    public static byte binaryStringToByte(String s){
        byte ret=0, total=0;
        for(int i=0; i<8; ++i){         
            ret = (s.charAt(7-i)=='1') ? (byte)(1 << i) : 0;
            total = (byte) (ret|total);
        }
        return total;
    } 

    /*2진수String to bytearray  변환*/
    public static byte[] binaryStringToByteArray(String s){
        int count=s.length()/8;
        byte[] b=new byte[count];
        for(int i=1; i<count; ++i){
            String t=s.substring((i-1)*8, i*8);
            b[i-1]=binaryStringToByte(t);
        }
        return b;
    } 

    /*bytearray to 16진수String 변환*/
    public static String toHexString(byte buf[]){
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < buf.length; i++) {
            sb.append(Integer.toHexString(0x0100 + (buf[i] & 0x00FF)).substring(1));
        }
        return sb.toString();
    } 

    /*16진수String to bytearray 변환*/
    public static byte[] hexToByteArray(String hex) {
        if (hex == null || hex.length() == 0) {
            return null;
        }
        hex = hex.replace("0x", "");

        byte[] ba = new byte[hex.length() / 2];
        for (int i = 0; i < ba.length; i++) {
            ba[i] = (byte) Integer.parseInt(hex.substring(2 * i, 2 * i + 2), 16);
        }
        return ba;
    }
 
반응형

댓글

💲 추천 글