123456789101112131415161718192021222324252627282930313233343536373839404142 |
- import 'dart:typed_data';
- class Convert {
- // String (Dart uses UTF-16) to bytes
- static stringToUint8(String string) async {
- var list = new List<int>();
- string.runes.forEach((rune) {
- if (rune >= 0x10000) {
- rune -= 0x10000;
- int firstWord = (rune >> 10) + 0xD800;
- list.add(firstWord >> 8);
- list.add(firstWord & 0xFF);
- int secondWord = (rune & 0x3FF) + 0xDC00;
- list.add(secondWord >> 8);
- list.add(secondWord & 0xFF);
- } else {
- list.add(rune >> 8);
- list.add(rune & 0xFF);
- }
- });
- Uint8List bytes = Uint8List.fromList(list);
- return bytes;
- }
- // Bytes to UTF-16 string
- static uint8ToString(Uint8List bytes) async {
- StringBuffer buffer = new StringBuffer();
- for (int i = 0; i < bytes.length;) {
- int firstWord = (bytes[i] << 8) + bytes[i + 1];
- if (0xD800 <= firstWord && firstWord <= 0xDBFF) {
- int secondWord = (bytes[i + 2] << 8) + bytes[i + 3];
- buffer.writeCharCode(
- ((firstWord - 0xD800) << 10) + (secondWord - 0xDC00) + 0x10000);
- i += 4;
- } else {
- buffer.writeCharCode(firstWord);
- i += 2;
- }
- }
- return buffer.toString();
- }
- }
|