日期:2014-05-20 浏览次数:20943 次
public class String2byteDemo { public static void main(String[] args) { String a ="2dsad132321321321"; byte[] b = new byte[100]; byte[] temp=a.getBytes(); int length=temp.length; if(length<=100){ for(int i=0;i<100;i++){ if(i<length){ b[i]=temp[i]; }else{ b[i]=0x00; } } } for(byte i:b){ System.out.print(i+","); } } }
------解决方案--------------------
public static void main(String[] args) {
String a="2afsabsdfgsdgcdbdfhrergfd1234fdsgsgsg";
byte[] b=new byte[100];
int i;
for(i=0;i<a.length();i++){
a.getBytes(i, i+1, b, i);
}
while(i<100){
b[i]=0x00;
i++;
}
System.out.println(Arrays.toString(b));
}