nmaxb = [0, 0x10000]
        while nmaxb[1] - nmaxb[0] > 1:
            block = nmaxb[0] + (nmaxb[1] - nmaxb[0]) // 2 - 1
            try:
                self.read_from_ndef_service(block)
            except Type3TagCommandError:
                nmaxb[1] = block + 1
            else:
                nmaxb[0] = block + 1

def _format(self, version, wipe):
        assert version is None or type(version) is int
        assert wipe is None or type(wipe) is int

        if self.sys != 0x12FC:
            log.warning("not an ndef tag and can not be made compatible")
            return False
        if version and version >> 4 != 1:
            log.warning("Type 3 Tag NDEF mapping major version must be 1")
            return False

        try:
            self.read_from_ndef_service(0)
        except Type3TagCommandError:
            log.warning("this tag does not have any usable data blocks")
            return False

        # To determine the total number of data blocks we start with
        # the assumption that it must be between 0 and 2**16, then try
        # reading in the middle and adjust the range depending on
        # whether the read was successful or not. So in each round we
        # have the smallest number that worked and the largest number
        # that didn't, obviously the end is when that difference is 1.
        """
        nmaxb = [0, 0x10000]
        while nmaxb[1] - nmaxb[0] > 1:
            block = nmaxb[0] + (nmaxb[1] - nmaxb[0]) // 2 - 1
            try:
                self.read_from_ndef_service(block)
            except Type3TagCommandError:
                nmaxb[1] = block + 1
            else:
                nmaxb[0] = block + 1
        """
        nmaxb = [0, 0x10000]
        while nmaxb[1] - nmaxb[0] > 1:
            print(nmaxb)
            block = nmaxb[0] + (nmaxb[1] - nmaxb[0]) // 2
            try:
                self.read_from_ndef_service(block)
            except Type3TagCommandError:
                nmaxb[1] = block
            else:
                nmaxb[0] = block

        nmaxb = nmaxb[0]

        # To get the number of blocks that can be read in one command
        # we just try to read with an increasing number of blocks.
        for nbr in range(1, 16):
            try:
                self.read_from_ndef_service(*(nbr*[0]))
            except Type3TagCommandError:
                nbr -= 1
                break

        # To get the number of blocks that can be written in one
        # command we do essentially the same as for nbr, just that to
        # preserve existing data we first read and then write it back.
        data = self.read_from_ndef_service(0)
        for nbw in range(1, 14):
            try:
                self.write_to_ndef_service(nbw*data, *(nbw*[0]))
            except Type3TagCommandError:
                nbw -= 1
                break

        # Tags with more than 4K memory require 3-byte block number
        # format. This reduces the maximum number of blocks in write.
        if nbw == 13 and nmaxb > 255:
            nbw = 12

        # We now have all information needed to create and write the
        # new attribute data to block number 0.
        attribute_data = bytearray(16)
        attribute_data[0:5] = pack(">BBBH", version, nbr, nbw, nmaxb)
        attribute_data[10] = 0x01 if nbw > 0 else 0x00
        attribute_data[14:16] = pack(">H", sum(attribute_data[0:14]))
        log.debug("set ndef attributes {}".format(hexlify(attribute_data)))
        self.write_to_ndef_service(attribute_data, 0)

        # If required, we will also overwrite the memory with the
        # 8-bit integer provided. This could take a while.
        if wipe is not None:
            data = bytearray(chr(wipe) * 16)
            while nmaxb > 0:
                self.write_to_ndef_service(data, nmaxb)
                nmaxb = nmaxb - 1

        return True