try {
byte[] b = source.getBytes(”UTF-8″);
if (b.length > length) {
System.arraycopy(b, 0, dst, 0, length);
} else {
System.arraycopy(b, 0, dst, 0, b.length);
for (int i = dst.length; i < length; i++) {
dst[i] = fillByte;
}
}
} catch (Exception e) {
for (int i = 0; i < length; i++) {
dst[i] = fillByte;
}
}
return dst;
}
7.将string转换成TLV编码方式的字节数组流
public static byte[] string2TLVbytes(int tag,String value) throws IOException{
int length=value.length();
byte[] tag1=int2bytes(tag,4);
byte[] length1=int2bytes(length,4);
byte[] value1=string2bytes(value,value.length(),(byte)0×00); byte[] buff=pack(tag1,length1,value1);
return buff;
}
8.将字节流转换成UTF-8字符串
public static String bytes2UTF8string(byte source[]) {
String dst = “”;
try {
dst = (new String(source, “UTF-8″));
} catch (UnsupportedEncodingException e) {
dst = “”;
}
return dst;
}
9.将字节流中的指定字节段转换成UTF-8字符型
public static String bytes2UTF8string2(byte b[],int offset,int len){ byte[] a=new byte[len];
for (int i=0;i<len;i++){
a[i]=b[offset];
offset++;
}