Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 568 #572

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ class MqttPublishPayload extends MqttPayload {

/// Converts an array of bytes to a character string.
static String bytesToStringAsString(typed.Uint8Buffer message) {
final sb = StringBuffer();
message.forEach(sb.writeCharCode);
return sb.toString();
return utf8.decode(message.toList());
}
}
18 changes: 18 additions & 0 deletions test/mqtt_client_base_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -997,4 +997,22 @@ void main() {
isTrue);
});
});
group('Conversions', () {
group('Bytes To String as String', () {
test('String 1', () {
final original = 'Hello, 世界!';
final buffer = typed.Uint8Buffer();
buffer.addAll(utf8.encode(original)); // Encode the string to bytes
final decoded = MqttPublishPayload.bytesToStringAsString(buffer);
expect(decoded == original, isTrue);
});
test('String 2', () {
final original = 'Seu próximo agendamento iniciará em 5 minutos.';
final buffer = typed.Uint8Buffer();
buffer.addAll(utf8.encode(original)); // Encode the string to bytes
final decoded = MqttPublishPayload.bytesToStringAsString(buffer);
expect(decoded == original, isTrue);
});
});
});
}