This paper addresses the challenge of integrating explicit hard constraints into the control barrier function (CBF) framework for ensuring safety in autonomous systems, including robots. We propose a novel data-driven method to derive CBFs from these hard constraints in practical scenarios. Our approach assumes that the forward invariant safe set is either a subset or equal to the constrained set. The process consists of two main steps. First, we randomly sample states within the constraint boundaries and identify inputs meeting the time derivative criteria of the hard constraint; this iterative process converges using the Jaccard index. Next, we formulate CBFs that enclose the safe set using the sampled boundaries. This enables the creation of a control-invariant safe set, approaching the maximum attainable level of control invariance. This approach, therefore, addresses the complexities posed by complex autonomous systems with constrained control input spaces, culminating in a control-invariant safe set that closely approximates the maximal control invariant set.