2013-02-07 80 views

回答

1

我拿了一類dart.io - > base64.dart,修改它一下,你瞧

如何使用:

var somestring = 'Hello dart!'; 

var base64string = Base64String.encode(somestring); 
var mystring = Base64String.decode(base64string); 

source on pastbin.com

source on gist.github.com

2

截至0.9.2的crypto

CryptoUtils已棄用。改爲使用dart:convert中的Base64 API和convert包中的十六進制API。

import 'dart:convert' show UTF8, BASE64; 

main() { 
    final str = 'https://dartpad.dartlang.org/'; 

    final base64 = BASE64.encode(UTF8.encode(str)); 
    print('base64: $base64'); 

    final str2 = UTF8.decode(BASE64.decode(base64)); 
    print(str2); 
    print(str == str2); 
} 

嘗試在DartPad

2

我會君特格拉斯的2016年4月10日,發佈評論,但我沒有信譽。正如他所說,現在你應該使用dart:convert庫。你必須結合幾個編解碼器才能從base64字符串中獲得一個utf8字符串,反之亦然。 This article表示fusing your codecs更快。

import 'dart:convert'; 

void main() { 
    var base64 = 'QXdlc29tZSE='; 
    var utf8 = 'Awesome!'; 

    // Combining the codecs 
    print(utf8 == UTF8.decode(BASE64.decode(base64))); 
    print(base64 == BASE64.encode(UTF8.encode(utf8))); 
    // Output: 
    // true 
    // true 

    // Fusing is faster, and you don't have to worry about reversing your codecs 
    print(utf8 == UTF8.fuse(BASE64).decode(base64)); 
    print(base64 == UTF8.fuse(BASE64).encode(utf8)); 
    // Output: 
    // true 
    // true 
} 

https://dartpad.dartlang.org/5c0e1cfb6d1d640cdc902fe57a2a687d

+0

爲什麼沒有這個內置鏢:轉換庫? – Pat

相關問題