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. |
3789
|
44 ## @end deftypefn |
|
45 ## |
|
46 ## @seealso{base2dec, dec2bin, bin2dec, hex2dec, dec2hex} |
|
47 |
3791
|
48 ## Author: Daniel Calvelo <dcalvelo@yahoo.com> |
3789
|
49 ## Adapted-by: Paul Kienzle <pkienzle@kienzle.powernet.co.uk> |
|
50 |
4492
|
51 function retval = dec2base (n, base, len) |
3789
|
52 |
4492
|
53 if (nargin < 2 || nargin > 3) |
|
54 usage ("dec2base (n, base [, len])"); |
3789
|
55 endif |
|
56 |
4492
|
57 if (prod (size (n)) != length (n)) |
|
58 error ("dec2base: cannot convert matrices."); |
|
59 elseif (any (n < 0 | n != fix (n))) |
|
60 error ("dec2base: can only convert non-negative integers.") |
3789
|
61 endif |
|
62 |
|
63 symbols = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
|
64 if (isstr (base)) |
|
65 symbols = base; |
|
66 base = length (symbols); |
|
67 if any (diff (sort (toascii (symbols))) == 0) |
|
68 error ("dec2base: symbols representing digits must be unique."); |
|
69 endif |
4030
|
70 elseif (! isscalar (base)) |
3789
|
71 error ("dec2base: cannot convert from several bases at once."); |
|
72 elseif (base < 2 || base > length (symbols)) |
|
73 error ("dec2base: base must be between 2 and 36 or a string of symbols"); |
|
74 endif |
|
75 |
5125
|
76 ## determine number of digits required to handle all numbers, can overflow |
|
77 ## by 1 digit |
|
78 max_len = round (log (max (max (n), 1)) ./ log (base)) + 1; |
4492
|
79 |
|
80 if (nargin == 3) |
|
81 max_len = max (max_len, len); |
|
82 endif |
3789
|
83 |
|
84 ## determine digits for each number |
4492
|
85 power = ones (length (n), 1) * (base .^ (max_len-1 : -1 : 0)); |
|
86 n = n(:) * ones (1, max_len); |
|
87 digits = floor (rem (n, base*power) ./ power); |
3789
|
88 |
|
89 ## convert digits to symbols |
4492
|
90 retval = reshape (symbols (digits+1), size (digits)); |
3789
|
91 |
5133
|
92 ## Check if the first element is the zero symbol. It seems possible |
|
93 ## that LEN is provided, and is less than the computed MAX_LEN and |
|
94 ## MAX_LEN is computed to be one larger than necessary, so we would |
|
95 ## have a leading zero to remove. But if LEN >= MAX_LEN, we should |
|
96 ## not remove any leading zeros. |
|
97 if ((nargin == 2 || (nargin == 3 && max_len > len)) |
|
98 && all (retval(:,1) == symbols(1))) |
5125
|
99 retval = retval(:,2:end); |
|
100 endif |
|
101 |
3789
|
102 endfunction |
5125
|
103 |
|
104 %!test |
|
105 %! s0=''; |
|
106 %! for n=1:13 |
|
107 %! for b=2:16 |
|
108 %! pp=dec2base(b^n+1,b); |
|
109 %! assert(dec2base(b^n,b),['1',s0,'0']); |
|
110 %! assert(dec2base(b^n+1,b),['1',s0,'1']); |
|
111 %! end |
|
112 %! s0=[s0,'0']; |
|
113 %! end |
|
114 |
|
115 %!test |
|
116 %! digits='0123456789ABCDEF'; |
|
117 %! for n=1:13 |
|
118 %! for b=2:16 |
|
119 %! pm=dec2base(b^n-1,b); |
|
120 %! assert(length(pm),n); |
|
121 %! assert(all(pm==digits(b))); |
|
122 %! end |
|
123 %! end |
|
124 |
|
125 %!test |
|
126 %! for b=2:16 |
|
127 %! assert(dec2base(0,b),'0'); |
|
128 %! end |
|
129 |
|
130 %!test |
|
131 %! assert(dec2base(2^51-1,2), |
|
132 %! '111111111111111111111111111111111111111111111111111'); |