[Index for tmp_for_tar/misc] [Return to Master Index]

winb

(tmp_for_tar/misc/winb.m)


Function Synopsis

s = winb( number, base, mat )

Help text

      s = winb( number(s), base [, mat] )

 Write "number" in base "base". 

 If 'mat' passed and is equal to the string "mat", then a matrix is
 returned whose elements are the successive digits. For negative
 numbers, only the first digit is negative.

 Otherwise, a string is returned, where the characters [0-9A-Z] are
 used as digits. Base should then be <= 36.

 If number has more than one element, 'winb' is called on each element
 and the results are stacked vertically.

 This function is sloow and must be re-written

 Last modified: April 2001



Listing of function file tmp_for_tar/misc/winb.m

##      s = winb( number(s), base [, mat] )
##  
## Write "number" in base "base". 
## 
## If 'mat' passed and is equal to the string "mat", then a matrix is
## returned whose elements are the successive digits. For negative
## numbers, only the first digit is negative.
##
## Otherwise, a string is returned, where the characters [0-9A-Z] are
## used as digits. Base should then be <= 36.
##
## If number has more than one element, 'winb' is called on each element
## and the results are stacked vertically.
##
## This function is sloow and must be re-written


## Author:        Etienne Grossmann  <etienne@isr.ist.utl.pt>
## Last modified: April 2001

function s = winb( number, base, mat )

  if prod(size(number))!=1,
    if nargin<3, 
      s = "" ;
      for i=1:prod(size(number)),
	s = [s; winb(number(i),base)] ;
      end
    else
      ## keyboard
      nd = ceil(log(max(abs(number))+1)/log(base));  # Number of digits
      s = zeros(prod(size(number)),nd) ;
      for i=1:prod(size(number)),
	tmp =  winb(number(i),base,mat);
	s(i,nd-length(tmp)+1:nd) = tmp ;
      end
    end
    return
  end

  if     number < 0, number = -number; sign = -1 ; 
  elseif number > 0, sign =  1 ; 
  end

  if base <1,
    printf("winb : base is negative or zero\n");
    keyboard
  end

  if exist("mat")!=1 || !strcmp(mat,"mat"),
    if number == 0,   s = "0", return ; end

    digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;

    if base > 36,
      printf("winb : base is too big. Fix 'digits' yourself\n");
      keyboard
    end
    ## floor(1+log(number)/log(base))
    ## keyboard
    s = blanks(floor(1+log(number)/log(base)));
    count = 1 ;
    while number,
      s(count++) = digits(1+rem(number,base)) ;
      number = floor(number/base) ;
    end
    s(1:count-1) = s(count-1:-1:1) ;
    s = deblank(s) ;
    if sign==-1, s = ["-",s] ; end
  else
    if number == 0,   s = 0, return ; end
    
    ## log(base)
    ## log(number)
    ## floor(1+log(number)/log(base))
    s = zeros(1,floor(1+log(number)/log(base)));
    count = 1 ;
    while number,
      s(count++) = rem(number,base) ;
      number = floor(number/base) ;
    end
    s(count-1) *= sign ;
    s(1:count-1) = s(count-1:-1:1) ;
  end

Produced by oct2html on Sat Apr 28 21:14:54 2001
Cross-Directory links are: ON