annotate scripts/strings/dec2base.m @ 7016:93c65f2a5668

[project @ 2007-10-12 06:40:56 by jwe]
author jwe
date Fri, 12 Oct 2007 06:41:26 +0000
parents 8b0cfeb06365
children a1dbe9d80eee
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
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
8 ## your option) any later version.
3789
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
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
17 ## <http://www.gnu.org/licenses/>.
3789
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
18
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
19 ## -*- texinfo -*-
4492
819e3c246702 [project @ 2003-08-29 17:09:38 by jwe]
jwe
parents: 4030
diff changeset
20 ## @deftypefn {Function File} {} dec2base (@var{n}, @var{b}, @var{len})
7001
8b0cfeb06365 [project @ 2007-10-10 18:02:59 by jwe]
jwe
parents: 6967
diff changeset
21 ## Return a string of symbols in base @var{b} corresponding to
3789
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
22 ## the nonnegative integer @var{n}.
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
23 ##
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
24 ## @example
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
25 ## dec2base (123, 3)
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
26 ## @result{} "11120"
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
27 ## @end example
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
28 ##
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
29 ## 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
30 ## 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
31 ##
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
32 ## 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
33 ## 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
34 ## as a symbol.
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
35 ##
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
36 ## @example
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
37 ## dec2base (123, "aei")
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
38 ## @result{} "eeeia"
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
39 ## @end example
4492
819e3c246702 [project @ 2003-08-29 17:09:38 by jwe]
jwe
parents: 4030
diff changeset
40 ##
819e3c246702 [project @ 2003-08-29 17:09:38 by jwe]
jwe
parents: 4030
diff changeset
41 ## The optional third argument, @var{len}, specifies the minimum
819e3c246702 [project @ 2003-08-29 17:09:38 by jwe]
jwe
parents: 4030
diff changeset
42 ## number of digits in the result.
5642
2618a0750ae6 [project @ 2006-03-06 21:26:48 by jwe]
jwe
parents: 5443
diff changeset
43 ## @seealso{base2dec, dec2bin, bin2dec, hex2dec, dec2hex}
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
3791
c1c532a0acb2 [project @ 2001-02-09 15:28:23 by jwe]
jwe
parents: 3789
diff changeset
46 ## Author: Daniel Calvelo <dcalvelo@yahoo.com>
3789
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
47 ## Adapted-by: Paul Kienzle <pkienzle@kienzle.powernet.co.uk>
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
48
4492
819e3c246702 [project @ 2003-08-29 17:09:38 by jwe]
jwe
parents: 4030
diff changeset
49 function retval = dec2base (n, base, len)
3789
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 if (nargin < 2 || nargin > 3)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5642
diff changeset
52 print_usage ();
3789
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
53 endif
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
54
6967
65a28e9de0a5 [project @ 2007-10-06 12:47:11 by jwe]
jwe
parents: 6046
diff changeset
55 if (numel (n) != length (n))
5407
2911127d0fe7 [project @ 2005-07-08 15:37:30 by jwe]
jwe
parents: 5400
diff changeset
56 n = n(:);
4492
819e3c246702 [project @ 2003-08-29 17:09:38 by jwe]
jwe
parents: 4030
diff changeset
57 elseif (any (n < 0 | n != fix (n)))
5407
2911127d0fe7 [project @ 2005-07-08 15:37:30 by jwe]
jwe
parents: 5400
diff changeset
58 error ("dec2base: can only convert non-negative integers")
3789
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
59 endif
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
60
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
61 symbols = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
5443
ec8c33dcd1bf [project @ 2005-09-08 01:40:57 by jwe]
jwe
parents: 5407
diff changeset
62 if (ischar (base))
3789
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
63 symbols = base;
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
64 base = length (symbols);
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
65 if any (diff (sort (toascii (symbols))) == 0)
5407
2911127d0fe7 [project @ 2005-07-08 15:37:30 by jwe]
jwe
parents: 5400
diff changeset
66 error ("dec2base: symbols representing digits must be unique");
3789
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
67 endif
4030
22bd65326ec1 [project @ 2002-08-09 18:58:13 by jwe]
jwe
parents: 3791
diff changeset
68 elseif (! isscalar (base))
5407
2911127d0fe7 [project @ 2005-07-08 15:37:30 by jwe]
jwe
parents: 5400
diff changeset
69 error ("dec2base: cannot convert from several bases at once");
3789
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
70 elseif (base < 2 || base > length (symbols))
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
71 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
72 endif
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
73
5125
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
74 ## 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
75 ## by 1 digit
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
76 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
77
819e3c246702 [project @ 2003-08-29 17:09:38 by jwe]
jwe
parents: 4030
diff changeset
78 if (nargin == 3)
819e3c246702 [project @ 2003-08-29 17:09:38 by jwe]
jwe
parents: 4030
diff changeset
79 max_len = max (max_len, len);
819e3c246702 [project @ 2003-08-29 17:09:38 by jwe]
jwe
parents: 4030
diff changeset
80 endif
3789
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
81
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
82 ## determine digits for each number
4492
819e3c246702 [project @ 2003-08-29 17:09:38 by jwe]
jwe
parents: 4030
diff changeset
83 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
84 n = n(:) * ones (1, max_len);
819e3c246702 [project @ 2003-08-29 17:09:38 by jwe]
jwe
parents: 4030
diff changeset
85 digits = floor (rem (n, base*power) ./ power);
3789
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
86
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
87 ## convert digits to symbols
4492
819e3c246702 [project @ 2003-08-29 17:09:38 by jwe]
jwe
parents: 4030
diff changeset
88 retval = reshape (symbols (digits+1), size (digits));
3789
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
89
5133
7191f6e086f9 [project @ 2005-02-08 19:17:41 by jwe]
jwe
parents: 5125
diff changeset
90 ## 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
91 ## 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
92 ## 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
93 ## 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
94 ## not remove any leading zeros.
7191f6e086f9 [project @ 2005-02-08 19:17:41 by jwe]
jwe
parents: 5125
diff changeset
95 if ((nargin == 2 || (nargin == 3 && max_len > len))
5400
c7e3cf2fce3e [project @ 2005-07-05 15:01:32 by jwe]
jwe
parents: 5307
diff changeset
96 && all (retval(:,1) == symbols(1)) && length (retval) != 1)
5125
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
97 retval = retval(:,2:end);
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
98 endif
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
99
3789
2a257be5e488 [project @ 2001-02-09 04:12:30 by jwe]
jwe
parents:
diff changeset
100 endfunction
5125
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
101
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
102 %!test
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
103 %! s0='';
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
104 %! for n=1:13
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
105 %! for b=2:16
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
106 %! pp=dec2base(b^n+1,b);
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
107 %! assert(dec2base(b^n,b),['1',s0,'0']);
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
108 %! assert(dec2base(b^n+1,b),['1',s0,'1']);
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
109 %! end
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
110 %! s0=[s0,'0'];
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
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
113 %!test
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
114 %! digits='0123456789ABCDEF';
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
115 %! for n=1:13
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
116 %! for b=2:16
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
117 %! pm=dec2base(b^n-1,b);
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
118 %! assert(length(pm),n);
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
119 %! assert(all(pm==digits(b)));
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
120 %! end
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
121 %! end
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
122
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
123 %!test
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
124 %! for b=2:16
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
125 %! assert(dec2base(0,b),'0');
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
126 %! end
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
127
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
128 %!test
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
129 %! assert(dec2base(2^51-1,2),
8bf86c0fad1b [project @ 2005-01-27 17:50:57 by jwe]
jwe
parents: 4492
diff changeset
130 %! '111111111111111111111111111111111111111111111111111');