formatFieldValue function

String formatFieldValue(
  1. String? binaryValue,
  2. int width
)

Format a binary field value for display.

Short values (<=4 bits) show as binary. Longer values show as hex. Uses ROHD radixString style: width'hHEX.

Implementation

String formatFieldValue(String? binaryValue, int width) {
  if (binaryValue == null || binaryValue.isEmpty) {
    return '';
  }
  if (binaryValue.contains('x')) {
    return "$width'hx";
  }
  if (binaryValue.contains('z')) {
    return "$width'hz";
  }
  if (width <= 4) {
    return "$width'b$binaryValue";
  }
  // Convert to hex.
  final bigInt = BigInt.tryParse(binaryValue, radix: 2);
  if (bigInt == null) {
    return binaryValue;
  }
  final hexDigits = (width + 3) ~/ 4;
  final hex = bigInt.toRadixString(16).padLeft(hexDigits, '0');
  return "$width'h$hex";
}