public static String toMask(BitSet ba, int length) {
        String hex = "";
        int hexPart;

        for (int i = 0; i < length; i += 8) {
            hexPart = 0;
            for (int j = 0; j < 8; j++) hexPart += ba.get(i + j) ? (1 << j) : 0;
            hex += stringPadding(Integer.toHexString(hexPart).toUpperCase(), 2, '0');
        }
        return hex;
    }