Convert Uint8List to String with Dart

后端 未结 4 1876

cipher.process returns an Uint8List which is a list of unsigned integers (0-255). I need to convert this Uint8List to a string that I can easily convert back to the same Uin

4条回答
  •  没有蜡笔的小新
    2021-01-07 19:13

    To answer the specific question. I haven't used cipher.process, and can't find the docs. But if it just returns raw bytes, then perhaps these would be best encoded as hexidecimal or base64.

    Have a look at CryptoUtils.bytesToBase64, and CryptoUtils.bytesToHex.

    To answer the general question in the title, if the Uint8List contains UTF8 text, then use UTF8.decode() from the dart:convert library. See the api docs.

    import 'dart:convert';
    
    main() {
      var encoded = UTF8.encode("Îñţérñåţîöñåļîžåţîờñ");
      var decoded = UTF8.decode([0x62, 0x6c, 0xc3, 0xa5, 0x62, 0xc3, 0xa6,
                               0x72, 0x67, 0x72, 0xc3, 0xb8, 0x64]);
    }
    

    String.fromCharCodes() takes a list of UTF-16 code units as input.

    Also see LATIN1 which will behave the same as String.fromCharCodes when the input is < 0xFF.

提交回复
热议问题