String s = "abcde";
byte[] b = s.getBytes();getBytes 
public byte[] getBytes() 
Convert this String into bytes according to the platform's default character encoding, storing the result into a new byte array.
Returns:the resultant byte array.

解决方案 »

  1.   

    char是16位的,byte是8位的,在Java里采用了Unicode,所以是16位的,而实际上,我们还要进行一些类似于C语言里对字节进行的一些操作,我们就需要获得8位的byte。如果只有普通的ASCII字符,原有String的长度和转换后的长度是相同的,否则,就可能出现不同,因为ASCII字符在转换的时候被放到了一个8位的byte中,而像汉字这样的字符转换时,会被放入两个byte中。
      

  2.   

    string类好像没有取一个字节的函数,有没有其他类可以做到这一点呢。有些类的函数只认byte类型的参数。