Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 317
Abdellatif Sraiti Avatar asked Oct 27 '25 08:10

Abdellatif Sraiti


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)),
  ));
like image 200
Richard Heap Avatar answered Oct 28 '25 22:10

Richard Heap



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!