Mercurial > hg > octave-avbm
changeset 4492:819e3c246702
[project @ 2003-08-29 17:09:38 by jwe]
author | jwe |
---|---|
date | Fri, 29 Aug 2003 17:09:38 +0000 |
parents | 96a25f032846 |
children | 49d88738a4a0 |
files | scripts/ChangeLog scripts/strings/dec2base.m scripts/strings/dec2bin.m scripts/strings/dec2hex.m |
diffstat | 4 files changed, 46 insertions(+), 24 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/ChangeLog +++ b/scripts/ChangeLog @@ -1,3 +1,8 @@ +2003-08-29 David Castelow <DCastelow@Airspan.com> + + * strings/dec2base.m, strings/dec2bin.m, strings/dec2hex.m: + Allow optional length argument. + 2003-08-28 John W. Eaton <jwe@bevo.che.wisc.edu> * polynomial/polyfit.m: Avoid calling flipud.
--- a/scripts/strings/dec2base.m +++ b/scripts/strings/dec2base.m @@ -18,7 +18,7 @@ ## 02111-1307, USA. ## -*- texinfo -*- -## @deftypefn {Function File} {} dec2base (@var{n}, @var{b}) +## @deftypefn {Function File} {} dec2base (@var{n}, @var{b}, @var{len}) ## Return a string of symbols in base @var{b} corresponding to the ## the nonnegative integer @var{n}. ## @@ -38,6 +38,9 @@ ## dec2base (123, "aei") ## @result{} "eeeia" ## @end example +## +## The optional third argument, @var{len}, specifies the minimum +## number of digits in the result. ## @end deftypefn ## ## @seealso{base2dec, dec2bin, bin2dec, hex2dec, dec2hex} @@ -45,16 +48,16 @@ ## Author: Daniel Calvelo <dcalvelo@yahoo.com> ## Adapted-by: Paul Kienzle <pkienzle@kienzle.powernet.co.uk> -function out = dec2base (d, base) +function retval = dec2base (n, base, len) - if (nargin != 2) - usage("dec2base (n, base)"); + if (nargin < 2 || nargin > 3) + usage ("dec2base (n, base [, len])"); endif - if (prod (size (d)) != length (d)) - error("dec2base: cannot convert matrices."); - elseif (any (d < 0 | d != fix (d))) - error("dec2base: can only convert non-negative integers.") + if (prod (size (n)) != length (n)) + error ("dec2base: cannot convert matrices."); + elseif (any (n < 0 | n != fix (n))) + error ("dec2base: can only convert non-negative integers.") endif symbols = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; @@ -71,14 +74,18 @@ endif ## determine number of digits required to handle all numbers - maxLen = floor (log (max (max (d), 1)) ./ log (base)) + 1; + max_len = floor (log (max (max (n), 1)) ./ log (base)) + 1; + + if (nargin == 3) + max_len = max (max_len, len); + endif ## determine digits for each number - power = ones (length (d), 1) * (base .^ (maxLen-1 : -1 : 0)); - d = d(:) * ones (1, maxLen); - digits = floor (rem (d, base*power) ./ power); + power = ones (length (n), 1) * (base .^ (max_len-1 : -1 : 0)); + n = n(:) * ones (1, max_len); + digits = floor (rem (n, base*power) ./ power); ## convert digits to symbols - out = reshape (symbols (digits+1), size (digits)); + retval = reshape (symbols (digits+1), size (digits)); endfunction
--- a/scripts/strings/dec2bin.m +++ b/scripts/strings/dec2bin.m @@ -18,7 +18,7 @@ ## 02111-1307, USA. ## -*- texinfo -*- -## @deftypefn {Function File} {} dec2bin (@var{n}) +## @deftypefn {Function File} {} dec2bin (@var{n}, @var{len}) ## Return a binary number corresponding the nonnegative decimal number ## @var{n}, as a string of ones and zeros. For example, ## @@ -29,19 +29,24 @@ ## ## If @var{n} is a vector, returns a string matrix, one row per value, ## padded with leading zeros to the width of the largest value. +## +## The optional second argument, @var{len}, specifies the minimum +## number of digits in the result. ## @end deftypefn ## ## @seealso{bin2dec, dec2base, base2dec, hex2dec, dec2hex} ## Author: Daniel Calvelo <dcalvelo@yahoo.com> -## 2001-02-02 Paul Kienzle <pkienzle@kienzle.powernet.co.uk> +## Adapted-by: Paul Kienzle <pkienzle@kienzle.powernet.co.uk> -function h = dec2bin (d) +function retval = dec2bin (n, len) - if (nargin != 1) - usage ("dec2bin (b)"); + if (nargin == 1) + retval = dec2base (n, 2); + elseif (nargin == 2) + retval = dec2base (n, 2, len); else - h = dec2base (d, 2); + usage ("dec2bin (n [, len])"); endif endfunction
--- a/scripts/strings/dec2hex.m +++ b/scripts/strings/dec2hex.m @@ -18,7 +18,7 @@ ## 02111-1307, USA. ## -*- texinfo -*- -## @deftypefn {Function File} {} dec2hex (@var{n}) +## @deftypefn {Function File} {} dec2hex (@var{n}, @var{len}) ## Return the hexadecimal string corresponding to the nonnegative ## integer @var{n}. For example, ## @@ -29,6 +29,9 @@ ## ## If @var{n} is a vector, returns a string matrix, one row per value, ## padded with leading zeros to the width of the largest value. +## +## The optional second argument, @var{len}, specifies the minimum +## number of digits in the result. ## @end deftypefn ## ## @seealso{hex2dec, dec2base, base2dec, bin2dec, dec2bin} @@ -36,12 +39,14 @@ ## Author: Daniel Calvelo <dcalvelo@yahoo.com> ## Adapted-by: Paul Kienzle <pkienzle@kienzle.powernet.co.uk> -function h = dec2hex (d) +function retval = dec2hex (n, len) - if (nargin != 1) - usage ("dec2hex (b)"); + if (nargin == 1) + retval = dec2base (n, 16); + elseif (nargin == 2) + retval = dec2base (n, 16, len); else - h = dec2base (d, 16); + usage ("dec2hex (n [, len])"); endif endfunction