Skip to content

Instantly share code, notes, and snippets.

@pingbird
Last active December 10, 2021 17:20
Show Gist options
  • Save pingbird/8eda75abdf13012a4778e3e09e8c5f1a to your computer and use it in GitHub Desktop.
Save pingbird/8eda75abdf13012a4778e3e09e8c5f1a to your computer and use it in GitHub Desktop.
import 'dart:convert';
import 'dart:math';
String charToHex(int c) => c.toRadixString(16).padLeft(2, '0');
String charListToHex(List<int> s) => s.map(charToHex).join(' ');
String shortToHex(int c) => c.toRadixString(16).padLeft(4, '0');
String shortListToHex(List<int> s) => s.map(shortToHex).join(' ');
List<List<int>> splitList(List<int> input, int size) {
final result = <List<int>>[];
for (var i = 0; i <= (input.length - 1) ~/ size; i++) {
var offset = i * size;
result.add(input.sublist(offset, min(input.length, offset + size)));
}
return result;
}
List<int> joinList(List<List<int>> input) => input.expand((e) => e).toList();
void main() {
final smiley = 'Hello, World!!! 😊';
print('Smiley: $smiley');
// Strings in Dart (and C#) are actually lists of UTF-16 code points:
print('UTF-16: ${shortListToHex(smiley.codeUnits)}');
// We can convert them to a list of UTF-8 bytes with utf8.encode:
final smileyChars = utf8.encode(smiley);
print('UTF-8: ${charListToHex(smileyChars)}');
// When sending streams of data over the wire, the data gets chunked:
final splitSmileyChars = splitList(smileyChars, 4);
print('Split up: ${splitSmileyChars.map(charListToHex).join(' | ')}');
// The naive solution to get our original string is to utf-8 decode each chunk
// into strings and concatenate them together. This blows up if a code point
// crosses the boundary of a split:
final decodedSplitSmiley = splitSmileyChars.map(utf8.decode).toList();
print('Strings: ${jsonEncode(decodedSplitSmiley)}');
print('Back together: ${decodedSplitSmiley.join()}');
// A more correct solution decodes the entire string at once:
final joinedSmileyChars = joinList(splitSmileyChars);
print('Back together v2: ${utf8.decode(joinedSmileyChars)}');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment