[java]代码库
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
/**
* Created with IntelliJ IDEA.
* User: ASUS
* Date: 14-6-27
* Time: 上午2:02
* To change this template use File | Settings | File Templates.
*/
public class TestGG {
public static byte[] charsToBytes(char[] src) {
CharBuffer charBuffer = CharBuffer.allocate(src.length);
charBuffer.put(src);
charBuffer.flip();
Charset cs = Charset.defaultCharset();
System.out.println(cs.name());
ByteBuffer byteBuffer = cs.encode(charBuffer);
return byteBuffer.array();
}
public static void main(String args[]) throws Exception{
String md5 = "0499AFA3432E9F2EBD81C134C1F5E4B3";
System.out.println(md5);
System.out.println(md5.length()); //32
System.out.println(md5.getBytes().length); //32
System.out.println(md5.toCharArray().length); //32
System.out.println(charsToBytes(md5.toCharArray()).length); //64
String hello = "测试字符测试字符测试字符测试字符测试字符测试字符测试字符测试字符";
System.out.println(hello);
System.out.println(hello.length()); //32
System.out.println(hello.getBytes().length); //64
System.out.println(hello.toCharArray().length);//32
System.out.println(charsToBytes(hello.toCharArray()).length);//64
}
}//源代码片段来自云代码http://yuncode.net
public static byte[] charsToBytes(char[] src) {
CharBuffer charBuffer = CharBuffer.allocate(src.length);
charBuffer.put(src);
charBuffer.flip();
Charset cs = Charset.forName("ASCII");
System.out.println(cs.name());
ByteBuffer byteBuffer = cs.encode(charBuffer);
return byteBuffer.array();
}//源代码片段来自云代码http://yuncode.net