Search code examples
flutterdarthexdecode

Function to iterate trough comma separated hex strings and decode them in dart/flutter


I need a little help with a function in dart/flutter I am trying to write.

There are bunch of HEX encoded strings separated by comma and joined together in one String.

For example: String input = 'HexEncodedStr1,HexEncodedStr2,HexEncodedStr3'

I need to decode each of those strings and output them in the same comma separated form:

String output = 'HexDecodedStr1,HexDecodedStr2,HexDecodedStr3'

Currently, I am using hex.dart package as string decoder but I am struggling to separate each encoded string before decoding it with hex.dart:

import 'package:hex/hex.dart';

//The decode function
String decode(hexString) {
  if (hexString != "") {
     hexString = HEX.decode(hexString);
     return hexString;
  } else {
     return "N/A";
  }
}

void main() {

  String test = decode('776f726c64,706c616e65740d0a');
  print(test); //world,planet

}

Solution

  • How about splitting the string and joining decoded parts afterwards?

    void main() {
      final decoded = '776f726c64,706c616e65740d0a'
          .split(',')
          .map(decode)
          .join(',');
      print(decoded); //world,planet
    }