How To Convert Unicode String To Proper String (Chinese language) in Flutter

Viewed 1268

I'm working on an app where I get data from an API and I'm getting a Chinese characters like that

"u9c9cu82b1u548cu7231"

it should be like this 鲜花和爱

How can I convert it?

1 Answers

Your string is in escaped unicode format. If it is always in this format you can simply strip out the escaping U characters and convert the hex to code points.

  var original = 'u9c9cu82b1u548cu7231';

  // split on 'u' and remove the first empty element
  var parts = original.split('u')..removeAt(0);

  // map from hex string to code point int, and create string
  print(String.fromCharCodes(
    parts.map<int>((hex) => int.parse(hex, radix: 16)),
  ));
Related