We develop and test a numerical code that provides a self-consistent deconvolution of energy-dependent hard X-ray (HXR) time profiles I(epsilon, t) into two HXR-producing electron components, i.e., directly precipitating and trap-precipitating electrons. These two HXR components can be physically distinguished because their energy-dependent time delays have an opposite sign. The deconvolution is based on the following model assumptions: (1) nonthermal electrons are injected from the acceleration site into coronal hare loops by an injection function f(E, alpha, t) that consists of synchronized pulses in energy E and pitch angle alpha, (2) electrons with initially small pitch angles (alpha less than or equal to alpha(0)) precipitate directly to the HXR emission site, (3) electrons with initially large pitch angles (alpha greater than or equal to alpha(0)) are temporarily trapped and precipitate after the collisional deflection time, and (4) nonthermal electrons lose their energy by Coulomb collisions and emit thick-target HXR bremsstrahlung in a high-density (fully collisional) site (near or inside the chromosphere). The numerical deconvolution provides a self-consistent determination of three physical parameters: (1) the electron time-of-flight distance l(TOF) between the acceleration/injection site and the HXR emission site, (2) the electron density n(e) in the trap region, and (3) the fraction of HXR-emitting electrons that precipitate directly, q(prec), which relates to the loss cone angle by q(prec)(alpha(0)) = (1 - cos alpha(0)) for isotropic pitch angle distributions. This yields the magnetic mirror ratio R = B-loss/B-inj = 1/sin(2) (alpha(0)) between the injection and loss cone site. With this method, we measure for the first time magnetic field ratios in coronal loops by means of HXR data. Based on this ratio, together with the knowledge of the photospheric field at the footpoint, a direct measurement of the magnetic field in the coronal acceleration region can be obtained. We simulate energy-dependent HXR data I(epsilon, t) with typical solar flare parameters (l(TOF) = 15,000 km, n(e) = 10(11) cm(-3), q(prec) = 0.5) and test the accuracy of the inversion code. We perform the inversion in 30 different simulations over the entire physically plausible parameter space and demonstrate that a satisfactory inversion of all three physical parameters l(TOF), n(e), and q(prec) is achieved in a density range of n(e) = 10(10)-10(12) cm(-3) for precipitation ratios of q(prec) = 0.1-0.9 and for signal-to-noise ratios of greater than or similar to 100 (requiring HXR count rates of greater than or similar to 10(4) counts s(-1)). Applications of this inversion method to solar flare observations in hard X-rays (CGRO/BATSE, Yohkoh/Hard X-Ray Telescope) and microwaves (Nobeyama) will be presented in subsequent papers.