代码之家  ›  专栏  ›  技术社区  ›  Andreas

奇怪的字符串.getByte(编码)

  •  1
  • Andreas  · 技术社区  · 17 年前

    我正在尝试:

    byte[] b = String.getBytes("ASCII") and get an UnsupportedEncodingException Exception
    
    String fName = new String(b,"ASCII");
    
    - got the same when used
    
    byte[] b = String.getByte("ISO8859_1");
    String fName = new String(b,"ISO8859_1");
    

    编辑:getByte更改为getBytes

    4 回复  |  直到 9 年前
        1
  •  7
  •   Jon Skeet    17 年前

    那段代码无法编译-它是 String.getBytes() String.getByte() ,它是一个实例方法而不是静态方法。它总是值得剪切和粘贴一个文件 真实的 示例,你已经得到工作(即使它只是一个虚拟的应用程序)。

    但是,假设您有类似的代码 编译时,您应该使用 "US-ASCII" "ISO-8859-1" 如文件中记录的名称所示 Charset JavaDoc .

        2
  •  2
  •   Simon Nickerson    17 年前

    我想你想要更像这样的东西:

    
    import java.io.UnsupportedEncodingException;
    
    
    public class Encoding
    {
        public static void main(String[] args) throws UnsupportedEncodingException
        {
            String s = "Hello world";
            byte[] b = s.getBytes("US-ASCII");
        }
    }
    
    
        3
  •  0
  •   dfa    17 年前

    according to javadoc 您必须使用: "US-ASCII"

        4
  •  0
  •   Tom    15 年前

    虽然值得注意 this still unresolved bug ...

    " 预期行为与实际行为 :

    使用编码时会出现可笑的性能下降 开发人员完全没有理由期待这样的结果