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