prefixToSubnet method

String? prefixToSubnet(
  1. int? prefix
)

Implementation

String? prefixToSubnet(int? prefix) {
  if (prefix == null || prefix == "") {
    return null;
  }
  String s = String.fromCharCodes(
      Iterable.generate(prefix, (_) => '1'.codeUnitAt(0)));
  s = s +
      String.fromCharCodes(
          Iterable.generate(32 - prefix, (_) => '0'.codeUnitAt(0)));
  List<String> result = [];
  for (int i = 0; i < 4; i++) {
    String chunk = s.substring(i * 8, i * 8 + 8);
    result.add(int.parse(chunk, radix: 2).toRadixString(10));
  }
  return result.join(".");
}