encode method

  1. @override
String encode(
  1. String input
)
override

Encodes the input string using the codec's specific algorithm.

input The plain text string to be encoded. Returns the encoded string.

Throws ArgumentError if the input is invalid for this codec. Throws FormatException if encoding fails due to format issues.

Implementation

@override
String encode(String input) {
  if (input.isEmpty) {
    throw ArgumentError('Input cannot be empty');
  }

  try {
    final bytes = utf8.encode(input);
    final hexString =
        bytes.map((byte) => byte.toRadixString(16).padLeft(2, '0')).join();

    return uppercase ? hexString.toUpperCase() : hexString;
  } catch (e) {
    throw FormatException('Failed to encode input to hex: $e');
  }
}