static Number toNumber(Object value,
                           Class targetClass) throws ConversionException {
        if (value instanceof Number) {
            return (Number)value;
        } else {
            String str = value.toString();
            if (str.startsWith(HEX_PREFIX)) {
                try {
                    return new BigInteger(str.substring(HEX_PREFIX.length()),
                                          HEX_RADIX);
                } catch (NumberFormatException nex) {
                    throw new ConversionException("Could not convert " + str +
                                                  " to " +
                                                  targetClass.getName() +
                                                  "! Invalid hex number.",
                                                  nex);
                }
            }

            try {
                Constructor constr = targetClass.getConstructor(CONSTR_ARGS);
                return (Number)constr.newInstance(new Object[] { str });
            } catch (InvocationTargetException itex) {
                throw new ConversionException("Could not convert " + str +
                                              " to " + targetClass.getName(),
                                              itex.getTargetException());
            } catch (Exception ex) {
                // Treat all possible exceptions the same way
                throw new ConversionException("Conversion error when trying to convert " +
                                              str + " to " +
                                              targetClass.getName(), ex);
            }
        }
    }