annotate scripts/strings/dec2base.m @ 7001:8b0cfeb06365

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