Skip to content

Instantly share code, notes, and snippets.

@jtmcdole
Last active August 24, 2023 22:45
Show Gist options
  • Save jtmcdole/476ac44f54c9f17a0cd587ed42b0ecfa to your computer and use it in GitHub Desktop.
Save jtmcdole/476ac44f54c9f17a0cd587ed42b0ecfa to your computer and use it in GitHub Desktop.
unicode fun in dart
===[🌈]===
length: 2
codeunits: 2
runes: 1
utf16: 0xd83c 0xdf08
utf8len: 4
utf8 bytes: 0xf0 0x9f 0x8c 0x88
===[Hello🌎]===
length: 7
codeunits: 7
runes: 6
utf16: 0x0048 0x0065 0x006c 0x006c 0x006f 0xd83c 0xdf0e
utf8len: 9
utf8 bytes: 0x48 0x65 0x6c 0x6c 0x6f 0xf0 0x9f 0x8c 0x8e
===[🏳️‍🌈]===
length: 6
codeunits: 6
runes: 4
utf16: 0xd83c 0xdff3 0xfe0f 0x200d 0xd83c 0xdf08
utf8len: 14
utf8 bytes: 0xf0 0x9f 0x8f 0xb3 0xef 0xb8 0x8f 0xe2 0x80 0x8d 0xf0 0x9f 0x8c 0x88
===[🏳️‍]===
length: 4
codeunits: 4
runes: 3
utf16: 0xd83c 0xdff3 0xfe0f 0x200d
utf8len: 10
utf8 bytes: 0xf0 0x9f 0x8f 0xb3 0xef 0xb8 0x8f 0xe2 0x80 0x8d
import 'dart:convert';
main() {
printIt(String sup) {
print('===[$sup]===');
print(' length: ${sup.length}');
print(' codeunits: ${sup.codeUnits.length}');
print(' runes: ${sup.runes.length}');
var hex =
sup.codeUnits.map((d) => '0x${d.toRadixString(16).padLeft(4, '0')}');
print(' utf16: ${hex.join(' ')}');
var utf8bytes = utf8.encode(sup);
hex =
utf8bytes.map((d) => '0x${d.toRadixString(16).padLeft(2, '0')}');
print(' utf8len: ${utf8bytes.length}');
print(' utf8 bytes: ${hex.join(' ')}');
}
printIt('🌈');
printIt('Hello🌎');
printIt('🏳️‍🌈');
printIt('🏳️‍');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment