zoukankan      html  css  js  c++  java
  • Java 输出指定编码的字符串

    Java Sting类有个根据byte,字符编码来输出的构造函数。
    以下为java文档中的解释。

    public String(byte[] bytes, String charsetName) throws UnsupportedEncodingException
    Constructs a new String by decoding the specified array of bytes using the specified charset. The length of the new String is a function of the charset, and hence may not be equal to the length of the byte array.

    The behavior of this constructor when the given bytes are not valid in the given charset is unspecified. The CharsetDecoder class should be used when more control over the decoding process is required.

    Parameters:
    bytes - the bytes to be decoded into characters
    charsetName - the name of a supported charset
    Throws:
    UnsupportedEncodingException - If the named charset is not supported
    Since:
    JDK1.1

    下面代码为转换的函数:

     1 /***
     2  * 按照指定编码转换字符串
     3  * @param str 需要转换的字符串
     4  * @param srcEncode 传递过来的字符串编码格式
     5  * @param dstEncode 需要转换成的编码格式
     6  * @return 指定编码格式的字符串
     7  */
     8 public String translate(String str, String srcEncode,String dstEncode) {
     9     String tempStr = "";
    10     try {
    11         tempStr = new String(str.getBytes(srcEncode),dstEncode);
    12     } catch (UnsupportedEncodingException e) {
    13         // TODO Auto-generated catch block
    14         e.printStackTrace();
    15     }
    16     return tempStr;
    17 }
  • 相关阅读:
    mysql数据类型介绍
    IO中同步、异步与阻塞、非阻塞的区别(转)
    法线
    C++配置坑-----openCv环境配置
    C++学习记录
    FBX SDK环境配置
    Unity调起外部程序cmd.exe等
    unity读写Excel表格
    Unity编辑器扩展
    Unity 读写文本 文件
  • 原文地址:https://www.cnblogs.com/tianyaxue/p/3145099.html
Copyright © 2011-2022 走看看