3789
|
1 ## Copyright (C) 2000 Daniel Calvelo |
|
2 ## |
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
|
9 ## |
|
10 ## Octave is distributed in the hope that it will be useful, but |
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 ## General Public License for more details. |
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
5307
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
3789
|
19 |
|
20 ## -*- texinfo -*- |
4492
|
21 ## @deftypefn {Function File} {} dec2base (@var{n}, @var{b}, @var{len}) |
3789
|
22 ## Return a string of symbols in base @var{b} corresponding to the |
|
23 ## the nonnegative integer @var{n}. |
|
24 ## |
|
25 ## @example |
|
26 ## dec2base (123, 3) |
|
27 ## @result{} "11120" |
|
28 ## @end example |
|
29 ## |
|
30 ## If @var{n} is a vector, return a string matrix with one row per value, |
|
31 ## padded with leading zeros to the width of the largest value. |
|
32 ## |
|
33 ## If @var{b} is a string then the characters of @var{b} are used as |
|
34 ## the symbols for the digits of @var{n}. Space (' ') may not be used |
|
35 ## as a symbol. |
|
36 ## |
|
37 ## @example |
|
38 ## dec2base (123, "aei") |
|
39 ## @result{} "eeeia" |
|
40 ## @end example |
4492
|
41 ## |
|
42 ## The optional third argument, @var{len}, specifies the minimum |
|
43 ## number of digits in the result. |
5642
|
44 ## @seealso{base2dec, dec2bin, bin2dec, hex2dec, dec2hex} |
3789
|
45 ## @end deftypefn |
|
46 |
3791
|
47 ## Author: Daniel Calvelo <dcalvelo@yahoo.com> |
3789
|
48 ## Adapted-by: Paul Kienzle <pkienzle@kienzle.powernet.co.uk> |
|
49 |
4492
|
50 function retval = dec2base (n, base, len) |
3789
|
51 |
4492
|
52 if (nargin < 2 || nargin > 3) |
6046
|
53 print_usage (); |
3789
|
54 endif |
|
55 |
4492
|
56 if (prod (size (n)) != length (n)) |
5407
|
57 n = n(:); |
4492
|
58 elseif (any (n < 0 | n != fix (n))) |
5407
|
59 error ("dec2base: can only convert non-negative integers") |
3789
|
60 endif |
|
61 |
|
62 symbols = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
5443
|
63 if (ischar (base)) |
3789
|
64 symbols = base; |
|
65 base = length (symbols); |
|
66 if any (diff (sort (toascii (symbols))) == 0) |
5407
|
67 error ("dec2base: symbols representing digits must be unique"); |
3789
|
68 endif |
4030
|
69 elseif (! isscalar (base)) |
5407
|
70 error ("dec2base: cannot convert from several bases at once"); |
3789
|
71 elseif (base < 2 || base > length (symbols)) |
|
72 error ("dec2base: base must be between 2 and 36 or a string of symbols"); |
|
73 endif |
|
74 |
5125
|
75 ## determine number of digits required to handle all numbers, can overflow |
|
76 ## by 1 digit |
|
77 max_len = round (log (max (max (n), 1)) ./ log (base)) + 1; |
4492
|
78 |
|
79 if (nargin == 3) |
|
80 max_len = max (max_len, len); |
|
81 endif |
3789
|
82 |
|
83 ## determine digits for each number |
4492
|
84 power = ones (length (n), 1) * (base .^ (max_len-1 : -1 : 0)); |
|
85 n = n(:) * ones (1, max_len); |
|
86 digits = floor (rem (n, base*power) ./ power); |
3789
|
87 |
|
88 ## convert digits to symbols |
4492
|
89 retval = reshape (symbols (digits+1), size (digits)); |
3789
|
90 |
5133
|
91 ## Check if the first element is the zero symbol. It seems possible |
|
92 ## that LEN is provided, and is less than the computed MAX_LEN and |
|
93 ## MAX_LEN is computed to be one larger than necessary, so we would |
|
94 ## have a leading zero to remove. But if LEN >= MAX_LEN, we should |
|
95 ## not remove any leading zeros. |
|
96 if ((nargin == 2 || (nargin == 3 && max_len > len)) |
5400
|
97 && all (retval(:,1) == symbols(1)) && length (retval) != 1) |
5125
|
98 retval = retval(:,2:end); |
|
99 endif |
|
100 |
3789
|
101 endfunction |
5125
|
102 |
|
103 %!test |
|
104 %! s0=''; |
|
105 %! for n=1:13 |
|
106 %! for b=2:16 |
|
107 %! pp=dec2base(b^n+1,b); |
|
108 %! assert(dec2base(b^n,b),['1',s0,'0']); |
|
109 %! assert(dec2base(b^n+1,b),['1',s0,'1']); |
|
110 %! end |
|
111 %! s0=[s0,'0']; |
|
112 %! end |
|
113 |
|
114 %!test |
|
115 %! digits='0123456789ABCDEF'; |
|
116 %! for n=1:13 |
|
117 %! for b=2:16 |
|
118 %! pm=dec2base(b^n-1,b); |
|
119 %! assert(length(pm),n); |
|
120 %! assert(all(pm==digits(b))); |
|
121 %! end |
|
122 %! end |
|
123 |
|
124 %!test |
|
125 %! for b=2:16 |
|
126 %! assert(dec2base(0,b),'0'); |
|
127 %! end |
|
128 |
|
129 %!test |
|
130 %! assert(dec2base(2^51-1,2), |
|
131 %! '111111111111111111111111111111111111111111111111111'); |