Mercurial > hg > octave-nkf
annotate scripts/strings/dec2base.m @ 12061:9b9aaacf194d release-3-2-x
dlmread: perform tilde expansion to filename argument
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Sun, 23 Aug 2009 11:09:17 +0200 |
parents | 923c7cb7f13f |
children | edf065b51fa9 |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 2000, 2001, 2002, 2003, 2005, 2006, 2007, 2009 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 |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9051
diff
changeset
|
22 ## the non-negative integer @var{n}. |
3789 | 23 ## |
24 ## @example | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
25 ## @group |
3789 | 26 ## dec2base (123, 3) |
27 ## @result{} "11120" | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
28 ## @end group |
3789 | 29 ## @end example |
30 ## | |
31 ## If @var{n} is a vector, return a string matrix with one row per value, | |
32 ## padded with leading zeros to the width of the largest value. | |
33 ## | |
34 ## If @var{b} is a string then the characters of @var{b} are used as | |
35 ## the symbols for the digits of @var{n}. Space (' ') may not be used | |
36 ## as a symbol. | |
37 ## | |
38 ## @example | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
39 ## @group |
3789 | 40 ## dec2base (123, "aei") |
41 ## @result{} "eeeia" | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
42 ## @end group |
3789 | 43 ## @end example |
4492 | 44 ## |
45 ## The optional third argument, @var{len}, specifies the minimum | |
46 ## number of digits in the result. | |
5642 | 47 ## @seealso{base2dec, dec2bin, bin2dec, hex2dec, dec2hex} |
3789 | 48 ## @end deftypefn |
49 | |
3791 | 50 ## Author: Daniel Calvelo <dcalvelo@yahoo.com> |
3789 | 51 ## Adapted-by: Paul Kienzle <pkienzle@kienzle.powernet.co.uk> |
52 | |
4492 | 53 function retval = dec2base (n, base, len) |
3789 | 54 |
4492 | 55 if (nargin < 2 || nargin > 3) |
6046 | 56 print_usage (); |
3789 | 57 endif |
58 | |
6967 | 59 if (numel (n) != length (n)) |
5407 | 60 n = n(:); |
4492 | 61 elseif (any (n < 0 | n != fix (n))) |
8664 | 62 error ("dec2base: can only convert non-negative integers"); |
3789 | 63 endif |
64 | |
65 symbols = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
5443 | 66 if (ischar (base)) |
3789 | 67 symbols = base; |
68 base = length (symbols); | |
69 if any (diff (sort (toascii (symbols))) == 0) | |
5407 | 70 error ("dec2base: symbols representing digits must be unique"); |
3789 | 71 endif |
4030 | 72 elseif (! isscalar (base)) |
5407 | 73 error ("dec2base: cannot convert from several bases at once"); |
3789 | 74 elseif (base < 2 || base > length (symbols)) |
75 error ("dec2base: base must be between 2 and 36 or a string of symbols"); | |
76 endif | |
77 | |
5125 | 78 ## determine number of digits required to handle all numbers, can overflow |
79 ## by 1 digit | |
80 max_len = round (log (max (max (n), 1)) ./ log (base)) + 1; | |
4492 | 81 |
82 if (nargin == 3) | |
83 max_len = max (max_len, len); | |
84 endif | |
3789 | 85 |
86 ## determine digits for each number | |
4492 | 87 power = ones (length (n), 1) * (base .^ (max_len-1 : -1 : 0)); |
88 n = n(:) * ones (1, max_len); | |
89 digits = floor (rem (n, base*power) ./ power); | |
3789 | 90 |
91 ## convert digits to symbols | |
4492 | 92 retval = reshape (symbols (digits+1), size (digits)); |
3789 | 93 |
5133 | 94 ## Check if the first element is the zero symbol. It seems possible |
95 ## that LEN is provided, and is less than the computed MAX_LEN and | |
96 ## MAX_LEN is computed to be one larger than necessary, so we would | |
97 ## have a leading zero to remove. But if LEN >= MAX_LEN, we should | |
98 ## not remove any leading zeros. | |
99 if ((nargin == 2 || (nargin == 3 && max_len > len)) | |
5400 | 100 && all (retval(:,1) == symbols(1)) && length (retval) != 1) |
5125 | 101 retval = retval(:,2:end); |
102 endif | |
103 | |
3789 | 104 endfunction |
5125 | 105 |
106 %!test | |
107 %! s0=''; | |
108 %! for n=1:13 | |
109 %! for b=2:16 | |
110 %! pp=dec2base(b^n+1,b); | |
111 %! assert(dec2base(b^n,b),['1',s0,'0']); | |
112 %! assert(dec2base(b^n+1,b),['1',s0,'1']); | |
113 %! end | |
114 %! s0=[s0,'0']; | |
115 %! end | |
116 | |
117 %!test | |
118 %! digits='0123456789ABCDEF'; | |
119 %! for n=1:13 | |
120 %! for b=2:16 | |
121 %! pm=dec2base(b^n-1,b); | |
122 %! assert(length(pm),n); | |
123 %! assert(all(pm==digits(b))); | |
124 %! end | |
125 %! end | |
126 | |
127 %!test | |
128 %! for b=2:16 | |
129 %! assert(dec2base(0,b),'0'); | |
130 %! end | |
131 | |
132 %!test | |
133 %! assert(dec2base(2^51-1,2), | |
134 %! '111111111111111111111111111111111111111111111111111'); |