Home > Net >  Dart encoding hex using Base64
Dart encoding hex using Base64

Time:01-28

I need to encode this

"f08b3616d575acffc9c9493a888eb4f915a6830f94101363ba01fc7c42011789"

to this

"8Is2FtV1rP/JyUk6iI60 RWmgw UEBNjugH8fEIBF4k="

using dart and base64 encode. I used this tool details

If it helps I manage to do it on Python using this oneliner

s = codecs.encode(codecs.decode(hex, 'hex'), 'base64').decode()

but have no clue for the equivalent in dart

CodePudding user response:

The hex codec isn't in the core dart:convert but is buried in a package also called convert. So, add convert to your pubspec.yaml, and then:

import 'dart:convert';

import 'package:convert/convert.dart';

void main() {
  final h = 'f08b3616d575acffc9c9493a888eb4f915a6830f94101363ba01fc7c42011789';
  final b = base64.encode(hex.decode(h));
  print(b);
}
  •  Tags:  
  • Related