1

i would like to convert 'Hellođź‘‹' to hexadecimal

The codeUnits are :

[72, 101, 108, 108, 111, 55357, 56395]

I would like to obtain : 48656c6c6ff09f918b

How to do that please ? I used some libs 'Pinenacl', 'hex' but I obtain a wrong hexa: 48656c6c6f3d4b

1

2 Answers 2

4

Your result are encoded in UTF-8 and not UTF-16 (which your code units are). So you need to first encode your String to UTF-8 data and then convert this into hex:

import 'dart:convert';

void main() {
  String string = String.fromCharCodes([72, 101, 108, 108, 111, 55357, 56395]);
  print(string); // Hellođź‘‹
  print(utf8.encode(string).map((e) => e.toRadixString(16)).join()); // 48656c6c6ff09f918b
}
0
1

You can use hex

List<int> list = utf8.encode("Hellođź‘‹");
String hex = HEX.encode(list);
print(hex);

result

48656c6c6ff09f918b
1
  • 1
    You don't need to convert your list (which are really a Uint8List) into Uint16List. Jul 18, 2022 at 10:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.