Re: inverse function of binary.pro [message #39960 is a reply to message #39959] |
Wed, 30 June 2004 07:20   |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
sebinjapan writes:
> I am using the function (best_)binary.pro available on David Fanning's web
> page:
> http://www.dfanning.com/misc_tips/binary_hex.html
>
> I would like to know if someone already wrote the inverse function of
> binary (a function that gives you the number corresponding to its binary
> representation)?
> Something like:
> IDL> print, inv_binary(binary(76.12))
> 76.12
For something quick and dirty, how about this:
FUNCTION Inverse_Binary, binaryNumber
s = Size(binaryNumber, /Dimensions)
bn = Reform(Long(binaryNumber), 8*s[1])
len = N_Elements(bn)
RETURN, Total(bn*2^Reverse(Indgen(len)))
END
Works for the two or three values I've tested. :-)
> btw: I think there is a minor error at the end of binary.pro. On little
> endian computer, it doesn't work with scalar of byte type. I think the
> last "if" test should be:
> if little_endian AND type NE 1 then $
> instead of
> if little_endian then $
Kevin Ivory gave me this little program, but I think you are
right about it. I've updated my web page with the change.
Thanks.
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
|
|
|