decode method
Decodes the input string using the codec's specific algorithm.
input
The encoded string to be decoded.
Returns the decoded plain text string.
Throws ArgumentError if the input is invalid for this codec. Throws FormatException if decoding fails due to format issues.
Implementation
@override
String decode(String input) {
if (input.isEmpty) {
throw ArgumentError('Input cannot be empty');
}
if (!isValidInput(input)) {
throw FormatException('Invalid hexadecimal input format');
}
try {
if (input.length % 2 != 0) {
throw FormatException('Hex string length must be even');
}
final bytes = <int>[];
for (int i = 0; i < input.length; i += 2) {
final hexByte = input.substring(i, i + 2);
final byte = int.parse(hexByte, radix: 16);
bytes.add(byte);
}
return utf8.decode(bytes);
} catch (e) {
throw FormatException('Failed to decode hex input: $e');
}
}