3789
|
1 ## Copyright (C) 2000 Daniel Calvelo |
|
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 |
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
|
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 |
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
|
17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
|
18 ## 02111-1307, USA. |
|
19 |
|
20 ## -*- texinfo -*- |
4492
|
21 ## @deftypefn {Function File} {} dec2base (@var{n}, @var{b}, @var{len}) |
3789
|
22 ## Return a string of symbols in base @var{b} corresponding to the |
|
23 ## the nonnegative integer @var{n}. |
|
24 ## |
|
25 ## @example |
|
26 ## dec2base (123, 3) |
|
27 ## @result{} "11120" |
|
28 ## @end example |
|
29 ## |
|
30 ## If @var{n} is a vector, return a string matrix with one row per value, |
|
31 ## padded with leading zeros to the width of the largest value. |
|
32 ## |
|
33 ## If @var{b} is a string then the characters of @var{b} are used as |
|
34 ## the symbols for the digits of @var{n}. Space (' ') may not be used |
|
35 ## as a symbol. |
|
36 ## |
|
37 ## @example |
|
38 ## dec2base (123, "aei") |
|
39 ## @result{} "eeeia" |
|
40 ## @end example |
4492
|
41 ## |
|
42 ## The optional third argument, @var{len}, specifies the minimum |
|
43 ## number of digits in the result. |
3789
|
44 ## @end deftypefn |
|
45 ## |
|
46 ## @seealso{base2dec, dec2bin, bin2dec, hex2dec, dec2hex} |
|
47 |
3791
|
48 ## Author: Daniel Calvelo <dcalvelo@yahoo.com> |
3789
|
49 ## Adapted-by: Paul Kienzle <pkienzle@kienzle.powernet.co.uk> |
|
50 |
4492
|
51 function retval = dec2base (n, base, len) |
3789
|
52 |
4492
|
53 if (nargin < 2 || nargin > 3) |
|
54 usage ("dec2base (n, base [, len])"); |
3789
|
55 endif |
|
56 |
4492
|
57 if (prod (size (n)) != length (n)) |
|
58 error ("dec2base: cannot convert matrices."); |
|
59 elseif (any (n < 0 | n != fix (n))) |
|
60 error ("dec2base: can only convert non-negative integers.") |
3789
|
61 endif |
|
62 |
|
63 symbols = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
|
64 if (isstr (base)) |
|
65 symbols = base; |
|
66 base = length (symbols); |
|
67 if any (diff (sort (toascii (symbols))) == 0) |
|
68 error ("dec2base: symbols representing digits must be unique."); |
|
69 endif |
4030
|
70 elseif (! isscalar (base)) |
3789
|
71 error ("dec2base: cannot convert from several bases at once."); |
|
72 elseif (base < 2 || base > length (symbols)) |
|
73 error ("dec2base: base must be between 2 and 36 or a string of symbols"); |
|
74 endif |
|
75 |
|
76 ## determine number of digits required to handle all numbers |
4492
|
77 max_len = floor (log (max (max (n), 1)) ./ log (base)) + 1; |
|
78 |
|
79 if (nargin == 3) |
|
80 max_len = max (max_len, len); |
|
81 endif |
3789
|
82 |
|
83 ## determine digits for each number |
4492
|
84 power = ones (length (n), 1) * (base .^ (max_len-1 : -1 : 0)); |
|
85 n = n(:) * ones (1, max_len); |
|
86 digits = floor (rem (n, base*power) ./ power); |
3789
|
87 |
|
88 ## convert digits to symbols |
4492
|
89 retval = reshape (symbols (digits+1), size (digits)); |
3789
|
90 |
|
91 endfunction |