Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dart - Encode and decode base64 string

Tags:

dart

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?

like image 704
Sergey Karasev Avatar asked Feb 07 '13 15:02

Sergey Karasev


People also ask

How do you decode base64 string darts?

To encode or decode Base64 in Dart, you can make use of the dart:convert library: import 'dart:convert'; For base64 decoding, use one of these 2 methods: String base64.

How do you encode string to base64 in darts?

Dart has a function in the package:crypto library, CryptoUtils. bytesToBase64 , which takes a list of bytes to encode as base64. In order to get the list of bytes from a Dart string, you can use the UTF8. encode() function in the dart:convert library.

How do you decode base64 PDF string in flutter?

This should convert base64 encoded pdf data into a byte array. import 'packages:dart/convert. dart'; List<int> pdfDataBytes = base64. decode(pdfBase64) .


1 Answers

As of 0.9.2 of the crypto package

CryptoUtils is deprecated. Use the Base64 APIs in dart:convert and the hex APIs in the convert package instead.

import 'dart:convert' show utf8, base64;

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

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

  final str2 = utf8.decode(base64.decode(encoded));
  print(str2);
  print(str == str2);
}

Try it in DartPad

like image 112
Günter Zöchbauer Avatar answered Oct 02 '22 22:10

Günter Zöchbauer