Reputation: 511846
I want to get the number of bytes in an arbitrary string. I can do it this way:
import 'dart:convert';
void main() {
String str = '你好';
final bytes = utf8.encode(str);
print(str.length); // 2
print(bytes.length); // 6
}
But is there a more direct way to do it that doesn't involve doing a conversion?
Upvotes: 4
Views: 1163
Reputation: 6171
There is no one byte <--> String mapping. You have to pick an encoding to figure out the number of bytes. UTF-8 is the most common.
Upvotes: 5