0

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

}

2 Answers 2

4

How about splitting the string and joining decoded parts afterwards?

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

You could use string.split(",");

https://api.flutter.dev/flutter/dart-core/String/split.html

String input = 'HexEncodedStr1,HexEncodedStr2,HexEncodedStr3'
var inputSplit = input.split(",");

Now you have a list of substring. I think that you can then you a for loop or foreach.

inputSplit.forEach((element) => print(decode(element);));

or:

 for(var i = 0; i < inputSplit.length; i++)
 {
    var oneHex = decode(inputSplit[i]);
    print(oneHex);
 }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.