We develop and test a numerical code that provides a self-consistent deconvolution of energy-dependent hard X-ray (HXR) time profiles I(e, 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 flare loops by an injection function f(E, α, t) that consists of synchronized pulses in energy E and pitch angle α, (2) electrons with initially small pitch angles (α ≤ α0) precipitate directly to the HXR emission site, (3) electrons with initially large pitch angles (α ≥ α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 lTOF between the acceleration/injection site and the HXR emission site, (2) the electron density ne in the trap region, and (3) the fraction of HXR-emitting electrons that precipitate directly, qprec, which relates to the loss cone angle by qprec(α0) = (1 - cos α0) for isotropic pitch angle distributions. This yields the magnetic mirror ratio R = Bloss/Binj = 1/sin2 (α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(e, t) with typical solar flare parameters (lTOF = 15,000 km, ne = 1011 cm-3, qprec = 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 lTOF, ne, and qprec is achieved in a density range of ne = 1010-1012 cm-3 for precipitation ratios of qprec = 0.1-0.9 and for signal-to-noise ratios of 100 (requiring HXR count rates of 104 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.