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?
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.
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.
This should convert base64 encoded pdf data into a byte array. import 'packages:dart/convert. dart'; List<int> pdfDataBytes = base64. decode(pdfBase64) .
As of 0.9.2 of the crypto
package
CryptoUtils
is deprecated. Use theBase64
APIs indart:convert
and the hex APIs in theconvert
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With