Ğecko talks / user support

Moi en Dart ça fonctionne ainsi:

static String _decodeHexString(String? hexString) {
  if (hexString == null) return '';

  try {
    // Remove any leading backslash-x prefix if present
    String cleanHex = hexString.replaceAll(r'\x', '');

    // Convert hex string to bytes
    List<int> bytes = [];
    for (int i = 0; i < cleanHex.length; i += 2) {
      if (i + 1 < cleanHex.length) {
        String hexByte = cleanHex.substring(i, i + 2);
        bytes.add(int.parse(hexByte, radix: 16));
      }
    }

    // Try UTF-8 first
    try {
      String result = utf8.decode(bytes);
      // Check if the result contains replacement characters
      if (!result.contains('�')) {
        return result;
      }
    } catch (_) {}

    // If UTF-8 fails or contains replacement characters, try Latin-1
    try {
      String result = latin1.decode(bytes);
      return result;
    } catch (_) {}

    // If both fail, fallback to UTF-8 with malformed allowed
    final result = utf8.decode(bytes, allowMalformed: true);
    return result;
  } catch (e) {
    // If decoding fails, return the original string
    log.e('Error decoding hex string: $e');
    return hexString;
  }
}
2 Likes