# HG changeset patch # User Rik # Date 1288535415 25200 # Node ID 7e8ce65f73cf27decb8395c0e7300bdde4cb9758 # Parent bc3fa8f6c4dc43a94f46e6f902b0136ec7c11051 Overhaul functions used to convert between number bases. diff --git a/scripts/ChangeLog b/scripts/ChangeLog --- a/scripts/ChangeLog +++ b/scripts/ChangeLog @@ -1,3 +1,10 @@ +2010-10-31 Rik + + * strings/base2dec.m, strings/bin2dec.m, strings/dec2base.m, + strings/dec2bin.m, strings/dec2hex.m, strings/hex2dec.m: + Improve docstrings, use more descriptive variable names, + add more tests for functions used to convert between bases. + 2010-10-31 Konstantinos Poulios * plot/__go_draw_axes__.m: Removing deprecated code handling the case diff --git a/scripts/strings/base2dec.m b/scripts/strings/base2dec.m --- a/scripts/strings/base2dec.m +++ b/scripts/strings/base2dec.m @@ -18,8 +18,8 @@ ## -*- texinfo -*- ## @deftypefn {Function File} {} base2dec (@var{s}, @var{b}) -## Convert @var{s} from a string of digits of base @var{b} into an -## integer. +## Convert @var{s} from a string of digits in base @var{b} to a decimal +## integer (base 10). ## ## @example ## @group @@ -43,13 +43,13 @@ ## @result{} 123 ## @end group ## @end example -## @seealso{dec2base, dec2bin, bin2dec, hex2dec, dec2hex} +## @seealso{dec2base, bin2dec, hex2dec} ## @end deftypefn ## Author: Daniel Calvelo ## Adapted-by: Paul Kienzle -function out = base2dec (d, base) +function out = base2dec (s, base) if (nargin != 2) print_usage (); @@ -59,35 +59,46 @@ if (ischar (base)) symbols = base; base = length (symbols); - if (any (diff (sort (toascii (symbols))) == 0)) + if (length (unique (symbols)) != base) error ("base2dec: symbols representing digits must be unique"); endif + if (any (isspace (symbols))) + error ("base2dec: whitespace characters are not valid symbols"); + endif elseif (! isscalar (base)) error ("base2dec: cannot convert from several bases at once"); elseif (base < 2 || base > length (symbols)) - error ("base2dec: base must be between 2 and 36 or a string of symbols"); + error ("base2dec: BASE must be between 2 and 36, or a string of symbols"); else - d = toupper (d); + s = toupper (s); endif ## Right justify the values before anything else. - d = strjust (d, "right"); + s = strjust (s, "right"); ## Lookup value of symbols in symbol table, with invalid symbols ## evaluating to NaN and space evaluating to 0. - table = NaN (256, 1); - table (toascii (symbols (1 : base))) = 0 : base-1; - table (toascii (" ")) = 0; - d = reshape (table (toascii (d)), size (d)); + table = NaN (1, 256); + table(toascii (symbols(1:base))) = 0 : base-1; + table(toascii (" ")) = 0; + s = table(toascii (s)); - ## Multiply the resulting digits by the appropriate power and - ## sum the rows. - out = d * (base .^ (columns(d)-1 : -1 : 0)'); + ## Multiply the resulting digits by the appropriate power + ## and sum the rows. + out = s * (base .^ (columns(s)-1 : -1 : 0)'); endfunction -%!error base2dec(); -%!error base2dec("11120"); -%!error base2dec("11120", 3, 4); %!assert(base2dec ("11120", 3), 123); %!assert(base2dec ("yyyzx", "xyz"), 123); +%!assert(base2dec ("-1", 2), NaN); + +%%Test input validation +%!error base2dec (); +%!error base2dec ("11120"); +%!error base2dec ("11120", 3, 4); +%!error base2dec ("11120", "1231"); +%!error base2dec ("11120", "12 3"); +%!error base2dec ("11120", ones(2)); +%!error base2dec ("11120", 37); + diff --git a/scripts/strings/bin2dec.m b/scripts/strings/bin2dec.m --- a/scripts/strings/bin2dec.m +++ b/scripts/strings/bin2dec.m @@ -19,8 +19,8 @@ ## -*- texinfo -*- ## @deftypefn {Function File} {} bin2dec (@var{s}) -## Return the decimal number corresponding to the binary number stored -## in the string @var{s}. For example: +## Return the decimal number corresponding to the binary number represented +## by the string @var{s}. For example: ## ## @example ## @group @@ -29,33 +29,30 @@ ## @end group ## @end example ## -## If @var{s} is a string matrix, returns a column vector of converted +## If @var{s} is a string matrix, return a column vector of converted ## numbers, one per row of @var{s}. Invalid rows evaluate to NaN. -## @seealso{dec2hex, base2dec, dec2base, hex2dec, dec2bin} +## @seealso{dec2bin, base2dec, hex2dec} ## @end deftypefn ## Author: Daniel Calvelo ## Adapted-by: Paul Kienzle -function d = bin2dec (h) +function d = bin2dec (s) - if (nargin == 1 && ischar (h)) - n = rows (h); - d = zeros (n, 1); - for i = 1:n - s = h(i,:); - s = s(! isspace (s)); - d(i) = base2dec (s, 2); - endfor + if (nargin == 1 && ischar (s)) + d = base2dec (s, 2); else print_usage (); endif endfunction -%!assert(bin2dec ("1110") == 14); +%!assert(bin2dec ("0000"), 0); +%!assert(bin2dec ("1110"), 14); +%!assert(bin2dec ("11111111111111111111111111111111111111111111111111111"), 2^53-1); +%%Test input validation %!error bin2dec (); +%!error bin2dec (1); +%!error bin2dec ("1", 2); -%!error bin2dec ("str", 1); - diff --git a/scripts/strings/dec2base.m b/scripts/strings/dec2base.m --- a/scripts/strings/dec2base.m +++ b/scripts/strings/dec2base.m @@ -45,7 +45,7 @@ ## ## The optional third argument, @var{len}, specifies the minimum ## number of digits in the result. -## @seealso{base2dec, dec2bin, bin2dec, hex2dec, dec2hex} +## @seealso{base2dec, dec2bin, dec2hex} ## @end deftypefn ## Author: Daniel Calvelo @@ -73,6 +73,9 @@ if (length (unique (symbols)) != base) error ("dec2base: symbols representing digits must be unique"); endif + if (any (isspace (symbols))) + error ("dec2base: whitespace characters are not valid symbols"); + endif elseif (! isscalar (base)) error ("dec2base: cannot convert from several bases at once"); elseif (base < 2 || base > length (symbols)) @@ -140,13 +143,14 @@ %! '111111111111111111111111111111111111111111111111111'); %!assert(dec2base(uint64(2)^63-1,16), '7FFFFFFFFFFFFFFF'); -%!Test input validation +%%Test input validation %!error dec2base () %!error dec2base (1) %!error dec2base (1, 2, 3, 4) %!error dec2base (-1) %!error dec2base (1.1) %!error dec2base (1,"ABA") +%!error dec2base (1,"A B") %!error dec2base (1, ones(2)) %!error dec2base (1, 1) %!error dec2base (1, 37) diff --git a/scripts/strings/dec2bin.m b/scripts/strings/dec2bin.m --- a/scripts/strings/dec2bin.m +++ b/scripts/strings/dec2bin.m @@ -18,9 +18,9 @@ ## . ## -*- texinfo -*- -## @deftypefn {Function File} {} dec2bin (@var{n}, @var{len}) -## Return a binary number corresponding to the non-negative decimal number -## @var{n}, as a string of ones and zeros. For example: +## @deftypefn {Function File} {} dec2bin (@var{d}, @var{len}) +## Return a binary number corresponding to the non-negative integer +## @var{d}, as a string of ones and zeros. For example: ## ## @example ## @group @@ -29,23 +29,23 @@ ## @end group ## @end example ## -## If @var{n} is a vector, returns a string matrix, one row per value, +## If @var{d} 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. -## @seealso{bin2dec, dec2base, base2dec, hex2dec, dec2hex} +## @seealso{bin2dec, dec2base, dec2hex} ## @end deftypefn ## Author: Daniel Calvelo ## Adapted-by: Paul Kienzle -function retval = dec2bin (n, len) +function b = dec2bin (d, len) if (nargin == 1) - retval = dec2base (n, 2); + b = dec2base (d, 2); elseif (nargin == 2) - retval = dec2base (n, 2, len); + b = dec2base (d, 2, len); else print_usage (); endif @@ -53,10 +53,9 @@ endfunction %!assert(strcmp (dec2bin (14), "1110")); - -%!error dec2bin (); - %!assert(strcmp (dec2bin (14, 6), "001110")); +%%Test input validation +%!error dec2bin (); %!error dec2bin (1, 2, 3); diff --git a/scripts/strings/dec2hex.m b/scripts/strings/dec2hex.m --- a/scripts/strings/dec2hex.m +++ b/scripts/strings/dec2hex.m @@ -18,9 +18,9 @@ ## . ## -*- texinfo -*- -## @deftypefn {Function File} {} dec2hex (@var{n}, @var{len}) +## @deftypefn {Function File} {} dec2hex (@var{d}, @var{len}) ## Return the hexadecimal string corresponding to the non-negative -## integer @var{n}. For example: +## integer @var{d}. For example: ## ## @example ## @group @@ -29,34 +29,33 @@ ## @end group ## @end example ## -## If @var{n} is a vector, returns a string matrix, one row per value, +## If @var{d} is a vector, return 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. -## @seealso{hex2dec, dec2base, base2dec, bin2dec, dec2bin} +## @seealso{hex2dec, dec2base, dec2bin} ## @end deftypefn ## Author: Daniel Calvelo ## Adapted-by: Paul Kienzle -function retval = dec2hex (n, len) +function h = dec2hex (d, len) if (nargin == 1) - retval = dec2base (n, 16); + h = dec2base (d, 16); elseif (nargin == 2) - retval = dec2base (n, 16, len); + h = dec2base (d, 16, len); else print_usage (); endif endfunction -%!assert(strcmp (tolower (dec2hex (2748)), "abc")); +%!assert(strcmpi (dec2hex (2748), "abc")); +%!assert(strcmpi (dec2hex (2748, 5), "00abc")); +%% Test input validation %!error dec2hex (); - -%!assert(strcmp (tolower (dec2hex (2748, 5)), "00abc")); - %!error dec2hex (1, 2, 3); diff --git a/scripts/strings/hex2dec.m b/scripts/strings/hex2dec.m --- a/scripts/strings/hex2dec.m +++ b/scripts/strings/hex2dec.m @@ -19,8 +19,8 @@ ## -*- texinfo -*- ## @deftypefn {Function File} {} hex2dec (@var{s}) -## Return the integer corresponding to the hexadecimal number stored -## in the string @var{s}. For example: +## Return the integer corresponding to the hexadecimal number represented +## by the string @var{s}. For example: ## ## @example ## @group @@ -33,25 +33,28 @@ ## ## If @var{s} is a string matrix, returns a column vector of converted ## numbers, one per row of @var{s}. Invalid rows evaluate to NaN. -## @seealso{dec2hex, base2dec, dec2base, bin2dec, dec2bin} +## @seealso{dec2hex, base2dec, bin2dec} ## @end deftypefn ## Author: Daniel Calvelo ## Adapted-by: Paul Kienzle -function d = hex2dec (h) +function d = hex2dec (s) - if (nargin != 1) + if (nargin == 1 && ischar (s)) + d = base2dec (s, 16); + else print_usage (); - else - d = base2dec (h, 16); endif endfunction +%!assert(hex2dec ("0000"), 0); +%!assert(hex2dec ("1FFFFFFFFFFFFF"), 2^53-1); %!assert(hex2dec ("12b") == 299 && hex2dec ("12B") == 299); +%%Test input validation %!error hex2dec (); +%!error hex2dec (1); +%!error hex2dec ("1", 2); -%!error hex2dec ("str", 1); -