Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
327 views
in Technique[技术] by (71.8m points)

Dart - Encode and decode base64 string

How to native convert string -> base64 and base64 -> string

I'm find only this bytes to base64string

would like this:

String Base64String.encode();
String Base64String.decode();

or ported from another language is easier?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I would comment on Günter's April 10th, 2016, post, but I don't have the reputation. As he says, you should use the dart:convert library, now. You have to combine a couple of codecs to get a utf8 string out of a base64 string and vice-versa. This article says that fusing your codecs is faster.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...