2013년 7월 21일 일요일

String 을 Byte 단위로 자르는 함수

출처 http://dreamchaser.tistory.com/10

/**
* String 을 BYTE 단위로 자르는 함수
* @author 박동규
* @param startIndex, length
* @return String 잘려진 문자열
*/
public String cutStringToByte (String str, int startIndex , int length) {
byte[] b1 = null;
byte[] b2 = null;
try{
if(str == null ) {
return "";
}
b1 = str.getBytes();
b2 = new byte[length] ;
if( length > (b1.length - startIndex) ) {
length = b1.length - startIndex;
}
System.arraycopy(b1,startIndex,b2,0,length);
} catch (Exception e) {
e.printStackTrace();
}
return new String(b2);
}