redDwarf
redDwarf

Reputation: 398

Dart/Flutter - Hex conversion?

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

Upvotes: 1

Views: 6741

Answers (2)

Miftakhul Arzak
Miftakhul Arzak

Reputation: 1867

You can use hex

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

result

48656c6c6ff09f918b

Upvotes: 1

julemand101
julemand101

Reputation: 31299

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
}

Upvotes: 4

Related Questions