[Java] 将字符编码转换成UTF-16LE码 →→→→→进入此内容的聊天室

来自 , 2021-02-06, 写在 Java, 查看 147 次.
URL http://www.code666.cn/view/a9a1d531
  1.         /** 16 位 UCS 转换格式,Little-endian(最高地址存放低位字节)字节顺序 */
  2.         public static final String UTF_16LE = "UTF-16LE";
  3.  
  4.         /**
  5.          * 将字符编码转换成UTF-16LE码
  6.          */
  7.         public String toUTF_16LE(String str) throws UnsupportedEncodingException {
  8.                 return this.changeCharset(str, UTF_16LE);
  9.         }
  10.  
  11.  
  12.         /**
  13.          * 字符串编码转换的实现方法
  14.          *
  15.          * @param str
  16.          *            待转换编码的字符串
  17.          * @param newCharset
  18.          *            目标编码
  19.          * @return
  20.          * @throws UnsupportedEncodingException
  21.          */
  22.         public String changeCharset(String str, String newCharset)
  23.                         throws UnsupportedEncodingException {
  24.                 if (str != null) {
  25.                         // 用默认字符编码解码字符串。
  26.                         byte[] bs = str.getBytes();
  27.                         // 用新的字符编码生成字符串
  28.                         return new String(bs, newCharset);
  29.                 }
  30.                 return null;
  31.         }

回复 "将字符编码转换成UTF-16LE码"

这儿你可以回复上面这条便签

captcha