【问题标题】:Convert String to UTF-8 bytes将字符串转换为 UTF-8 字节
【发布时间】:2018-04-01 14:15:52
【问题描述】:

如何将 Ceylon 中的字符串编码为 UTF-8 字节?

value string = "my_string";
[Byte*] bytes = string.______;

【问题讨论】:

    标签: string ceylon


    【解决方案1】:

    使用ceylon.buffer.charset。

    import ceylon.buffer.charset {
        utf8
    }
    
    shared void run() {
        value string = "my_string";
        List<Byte> bytes = utf8.encode(string);
        Byte[] bytesSequence = bytes.sequence(); // in case a List isn’t enough
    }
    

    Try it!

    【讨论】:

    • 我了解锡兰最佳实践,为什么encode 返回List&lt;Byte&gt; 而不是Byte[]?我认为List 用于可能可变的事物。
    • @drhagen 不,List 接口根本没有说明可变性。 List 可能是可变的或不可变的。 OTOH,我不太清楚为什么encode() 不只是返回一个惰性流,即{Byte*}。
    猜你喜欢
    • 1970-01-01
    • 2014-03-07
    • 1970-01-01
    • 2010-11-03
    • 1970-01-01
    • 2011-07-01
    • 1970-01-01
    相关资源
    最近更新 更多