본문 바로가기
프로그램/Java

[Java] byte[] -> hex, hex -> byte[]

by 로드러너 2013. 11. 15.
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

1. byte[] 을 16진수 스트링으로 변환

     byte[] buffer; 

     StringBuffer sb = new StringBuffer( buffer.length * 2 );
     String hexaDecimal;
 
     for( int x=0;x<buffer.length;x++ ){
          hexD=”0″ + Integer.toHexString( 0xff & buffer[x] );
          sb.append( hexaDecimal.substring( hexaDecimal.length()-2) );
     }
 
     sb.toString();

2. 16진수 스트링을 byte[] 로 변환

     String hexaDecimal = “16진수 스트링”;

     byte[] buffer = new byte[hexaDecimal.length()/2];
     for( int i=0;i<buffer.length;i++ ){
          buffer[i] = (byte)Integer.parseInt(hexaDecimal.substring(2*i, 2*i+2), 16);
     }