Search code examples
stringceylon

Convert String to UTF-8 bytes


How do I encode a String in Ceylon as UTF-8 bytes?

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

Solution

  • Use 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!