;+ ; Project : SOHO - CDS ; ; Name : BIN2DEC_JWL ; ; Purpose : Convert binary representation to decimal integer. ; ; Explanation : The binary representation of a decimal number is converted ; to a decimal integer and can be displayed or returned or ; both or neither. ; ; Use : IDL> bin2dec_jwl, binary [, decimal, /quiet] ; ; Inputs : binary - the binary representation to convert. It can either ; be a string of zeros and ones or an array with each ; element a zero or one. ; eg bin2dec_jwl,'01010101' or ; bin2dec_jwl,['1','0','1','0','1','0','1','0'] or ; bin2dec_jwl,[1,0,1,0,1,0,1,0] ; The MSB is assumed to come first ; ; Opt. Inputs : None ; ; Outputs : See below ; ; Opt. Outputs: decimal - the decimal integer equivalent of the input. ; ; Keywords : quiet - unless given the decimal number is printed to the ; terminal ; ; Calls : None ; ; Restrictions: Input must be a string or an array of integers or strings. ; ; Side effects: None ; ; Category : Utils, Numerical ; ; Prev. Hist. : None ; ; Written : C D Pike, RAL, 7-Oct-93 ; ; Modified : Johnny Lin , 26 Feb 2002, to correct ; an error for the case of input being a 1-element array ; (i.e. BIN2DEC_JWL,['01010101']). Stricter error condition ; is also added. Passed reasonably passable tests. ; ; Version : Version 1, 7-Oct-93 ; ; \$Id: bin2dec_jwl.pro,v 1.1 2002/03/12 03:14:56 johnny Exp \$ ;- pro bin2dec_jwl,inp,out,quiet=quiet ; ; set stricter on error condition ; ON_ERROR, 0 ; ; initialise output and find size of input ; out = 0L n = n_elements(inp) ; ; is input a single string? If so break it up. (note we have to ; check for case of a 1 element array, i.e. n not scalar, or else ; for loop won't work) ; if n eq 1 and datatype(inp) eq 'STR' then begin n = strlen(inp) inp1 = intarr(n) if (SIZE(n, /N_Dimensions) ne 0) then begin if (N_ELEMENTS(n) ne 1) then MESSAGE, 'error--bad n' n = n[0] endif for i=0,n-1 do inp1(i) = strmid(inp,i,1) inp = inp1 endif ; ; is input a multiple string? ; if n gt 1 and datatype(inp) eq 'STR' then begin inp = fix(inp) endif ; ; switch array around for convenience ; x = reverse(byte(inp)) ; ; calculate integer ; for i=0,n-1 do out = out + long(x(i))*2L^i ; ; if not silenced then report ; if not keyword_set(quiet) then print,out end