I know this has been asked before and Ive searched the previous threads. However the only equation I can find gave a wrong answer when I tried to implement it.

So given the z-buffer value and the near and far plane distances , how do I get the distance to the point?

You mean how far into the scene a specific Z-buffer value is located?

This is a piece of code I have written. It takes the near plane, far plane and the Z-buffer value, and returns the distance from the near plane. If you want the distance from the viewpoint, just add the near plane to the result.

double convertZ(double n, double f, double z)
{
// Convert Z from [0, 1] to [-1, 1]
double wz = (2.0 * z) - 1.0;

double a = -(f - n) / (2.0 * f * n);
double b = (f + n) / (2.0 * f * n);