Skip to content

Commit

Permalink
Issue 568
Browse files Browse the repository at this point in the history
  • Loading branch information
shamblett committed Dec 2, 2024
1 parent c14c65f commit da56953
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
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);
});
});
});
}

0 comments on commit da56953

Please sign in to comment.