📜  decode, encode base64 dart (1)

📅  最后修改于: 2023-12-03 14:40:41.688000             🧑  作者: Mango

Decode/Encode Base64 in Dart

Introduction

Base64 is a method for encoding binary data in such a way that it can be safely transferred over communication channels that are designed to handle textual data. In Dart, there are built-in functions for encoding and decoding Base64 data.

Decoding Base64

The built-in function for decoding Base64 data in Dart is base64.decode(). This function takes a String input encoded in Base64 and returns the decoded data as a List<int>.

import 'dart:convert';

void main() {
  String base64EncodedData = "VGhpcyBpcyBhIHRlc3Q=";
  List<int> decodedData = base64.decode(base64EncodedData);

  print(decodedData); // Output: [84, 104, 105, 115, 32, 105, 115, 32, 97, 32, 116, 101, 115, 116]
}
Encoding Base64

The built-in function for encoding binary data in Base64 format in Dart is base64.encode(). This function takes a List<int> of binary data and returns the data encoded in Base64 as a String.

import 'dart:convert';

void main() {
  List<int> binaryData = [84, 104, 105, 115, 32, 105, 115, 32, 97, 32, 116, 101, 115, 116];
  String base64EncodedData = base64.encode(binaryData);

  print(base64EncodedData); // Output: VGhpcyBpcyBhIHRlc3Q=
}
Conclusion

In summary, Dart provides built-in functions for encoding and decoding Base64 data. These functions make it easy to transmit binary data in a safe and reliable manner over communication channels that can only handle textual data.