%u0BB5%u0BA3%u0B95%u0BCD%u0B95%u0BAE%u0BCD
Above is unicode with hex characters string Need to convert that to readable text When decoded, the above text will return வணக்கம் meaning welcome
CodePudding user response:
If you want a hard-coded string, as noted in Special characters in Flutter
and in the Dart Language Tour, you can use \u
to specify Unicode code points:
var welcome = '\u0BB5\u0BA3\u0B95\u0BCD\u0B95\u0BAE\u0BCD';
If you are given a string '%u0BB5%u0BA3%u0B95%u0BCD%u0B95%u0BAE%u0BCD'
and need to convert it dynamically at runtime, then you will need to:
- Split the string into
%uXXXX
components. - Parse the
XXXX
portion as a hexadecimal integer to get the code point. - Construct a
String
from the code points.
void main() {
var s = '%u0BB5%u0BA3%u0B95%u0BCD%u0B95%u0BAE%u0BCD';
var re = RegExp(r'%u(?<codePoint>[0-9A-Fa-f]{4})');
var matches = re.allMatches(s);
var codePoints = [
for (var match in matches)
int.parse(match.namedGroup('codePoint')!, radix: 16),
];
var decoded = String.fromCharCodes(codePoints);
print(decoded); // Prints: வணக்கம்
}
Edit: An adjusted version that can handle strings with a mixture of encoded code points and unencoded characters:
void main() {
var s = '%u0BB5%u0BA3%u0B95%u0BCD%u0B95%u0BAE%u0BCD'
' hello world! '
'%u0BB5%u0BA3%u0B95%u0BCD%u0B95%u0BAE%u0BCD';
var re = RegExp(r'(%u(?<codePoint>[0-9A-Fa-f]{4}))|.');
var matches = re.allMatches(s);
var codePoints = <int>[];
for (var match in matches) {
var codePoint = match.namedGroup('codePoint');
if (codePoint != null) {
codePoints.add(int.parse(codePoint, radix: 16));
} else {
codePoints = match.group(0)!.runes.toList();
}
}
var decoded = String.fromCharCodes(codePoints);
print(decoded); // Prints: வணக்கம் hello world! வணக்கம்
}