comparison scripts/strings/dec2base.m @ 5407:2911127d0fe7

[project @ 2005-07-08 15:37:30 by jwe]
author jwe
date Fri, 08 Jul 2005 15:37:30 +0000
parents c7e3cf2fce3e
children ec8c33dcd1bf
comparison
equal deleted inserted replaced
5406:c49aec8a9080 5407:2911127d0fe7
53 if (nargin < 2 || nargin > 3) 53 if (nargin < 2 || nargin > 3)
54 usage ("dec2base (n, base [, len])"); 54 usage ("dec2base (n, base [, len])");
55 endif 55 endif
56 56
57 if (prod (size (n)) != length (n)) 57 if (prod (size (n)) != length (n))
58 error ("dec2base: cannot convert matrices."); 58 n = n(:);
59 elseif (any (n < 0 | n != fix (n))) 59 elseif (any (n < 0 | n != fix (n)))
60 error ("dec2base: can only convert non-negative integers.") 60 error ("dec2base: can only convert non-negative integers")
61 endif 61 endif
62 62
63 symbols = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 63 symbols = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
64 if (isstr (base)) 64 if (isstr (base))
65 symbols = base; 65 symbols = base;
66 base = length (symbols); 66 base = length (symbols);
67 if any (diff (sort (toascii (symbols))) == 0) 67 if any (diff (sort (toascii (symbols))) == 0)
68 error ("dec2base: symbols representing digits must be unique."); 68 error ("dec2base: symbols representing digits must be unique");
69 endif 69 endif
70 elseif (! isscalar (base)) 70 elseif (! isscalar (base))
71 error ("dec2base: cannot convert from several bases at once."); 71 error ("dec2base: cannot convert from several bases at once");
72 elseif (base < 2 || base > length (symbols)) 72 elseif (base < 2 || base > length (symbols))
73 error ("dec2base: base must be between 2 and 36 or a string of symbols"); 73 error ("dec2base: base must be between 2 and 36 or a string of symbols");
74 endif 74 endif
75 75
76 ## determine number of digits required to handle all numbers, can overflow 76 ## determine number of digits required to handle all numbers, can overflow