Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I encode a Dart string in base64? [duplicate]

Tags:

dart

I'm working with an API that requires data encoded in base64. How can I encode a simple string in base64?

like image 716
Timothy Armstrong Avatar asked Apr 11 '13 19:04

Timothy Armstrong


People also ask

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 I encode a string in base64?

If we were to Base64 encode a string we would follow these steps: Take the ASCII value of each character in the string. Calculate the 8-bit binary equivalent of the ASCII values. Convert the 8-bit chunks into chunks of 6 bits by simply re-grouping the digits.

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.

What is double base64 encoding?

Double-base-64-encoded strings are regular, because there is a finite amount of byte sequences that properly base64-encode a base64-encoded message. You can check if something is base64-encoded once since you can validate each set of four characters.


1 Answers

There is no need to use the crypto package since the core libraries provide built-in support for base64 encoding and decoding.

https://api.dartlang.org/stable/2.1.0/dart-convert/dart-convert-library.html

import 'dart:convert';  main() {   final str = "Hello world";   final bytes = utf8.encode(str);   final base64Str = base64.encode(bytes);   print(base64Str); } 
like image 160
Ben Avatar answered Oct 19 '22 06:10

Ben