Viewing contents of file '../idllib/contrib/fanning/binary.pro'
FUNCTION BINARY, number
; This function returns the binary representation
; of byte, integer, and long integer numbers.
; What kind of number is this?
thisType = SIZE(number, /Type)
CASE thisType OF
1: BEGIN ; Byte value
bin = STRARR(8)
FOR j=0,7 DO BEGIN
powerOfTwo = 2L^j
IF (LONG(number) AND powerOfTwo) EQ powerOfTwo THEN $
bin(j) = '1' ELSE bin(j) = '0'
ENDFOR
ENDCASE
2: BEGIN ; Integer value.
bin = STRARR(16)
FOR j=0,15 DO BEGIN
powerOfTwo = 2L^j
IF (LONG(number) AND powerOfTwo) EQ powerOfTwo THEN $
bin(j) = '1' ELSE bin(j) = '0'
ENDFOR
ENDCASE
3: BEGIN ; Long integer value.
number = LONG(number)
bin = STRARR(32)
FOR j=0,31 DO BEGIN
powerOfTwo = 2L^j
IF (LONG(number) AND powerOfTwo) EQ powerOfTwo THEN $
bin(j) = '1' ELSE bin(j) = '0'
ENDFOR
ENDCASE
ELSE: ok = Dialog_Message('Only BYTE, INTEGER, and LONG values allowed.')
ENDCASE
RETURN, Reverse(bin)
END