Difference between UTF-8 and UTF-16?

前端 未结 5 882
感情败类
感情败类 2020-11-28 18:34

Difference between UTF-8 and UTF-16? Why do we need these?

MessageDigest md = MessageDigest.getInstance(\"SHA-256\");
String text = \"This is some text\";

m         


        
5条回答
  •  暖寄归人
    2020-11-28 18:58

    This is unrelated to UTF-8/16 (in general, although it does convert to UTF16 and the BE/LE part can be set w/ a single line), yet below is the fastest way to convert String to byte[]. For instance: good exactly for the case provided (hash code). String.getBytes(enc) is relatively slow.

    static byte[] toBytes(String s){
            byte[] b=new byte[s.length()*2];
            ByteBuffer.wrap(b).asCharBuffer().put(s);
            return b;
        }
    

提交回复
热议问题