bytesToString static method

String bytesToString(
  1. Uint8List bytes, {
  2. bool trimNull = true,
})

Convert bytes to ASCII string.

Parameters:

  • bytes: Byte array containing ASCII characters
  • trimNull: Remove null terminators (default: true)

Returns: ASCII string

Example:

final bytes = await client.readHoldingRegistersBytes(1, 300, 10);
final deviceName = DataConverter.bytesToString(bytes);

Implementation

static String bytesToString(Uint8List bytes, {bool trimNull = true}) {
  if (trimNull) {
    final nullIndex = bytes.indexOf(0);
    if (nullIndex >= 0) {
      bytes = bytes.sublist(0, nullIndex);
    }
  }
  return String.fromCharCodes(bytes);
}