The way that I would approach the problem is like this:
Design a 40 V power supply, with feedback, probably using a switch-mode IC. That will have a feedback point that is held at 1.25 V, as long as you use and adjustable voltage regulator such as LM2576HVT-ADJ
https://www.electro-tech-online.com/custompdfs/2009/05/LM2576HV.pdf
Convert the 4 - 20 mA to 1.25 - 6.25 V and buffer so that there is little impedance. A 250 Ω resistor and a op amp that gives a gain of 1.25 will do.
Connect the buffered 1.25 - 6.25 V to the feedback point using a suitable resistor, using the value of the feedback resistors for you calculations. For instance, if you have 10k and 310k as your feedback resistors, you need 38.75 k (calculation below)
That will give you 0 - 40 V for 4 - 20 mA. It will be reversed, so 4mA will give you 40 V and 20 mA will give you 0V, so sort it out in software or the op amp.
Calculation, if anyone cares:-
1.25 / 40 = 1/32 so that is why 10k and 310k would work as feedback resistors.
When the op amp output is 1.25 V, there is no voltage difference between that and the feedback point, so resistance doesn't matter.
When the op amp output is 6.25 V, the voltage output is 0 V, but the feedback point is still 1.25 V. 125 µA will flow through the 10k to ground. 4.0322 µA will flow through the 310 k to the voltage output, which is also at ground. Total is 129.0322 µA.
Control voltage is at 6.25 V, 5V above the feedback point. So the 129.0322 µA is given by a 38.75k resistor.