annotate scripts/strings/dec2base.m @ 5307:4c8a2e4e0717

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