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 -*- |
|
21 ## @deftypefn {Function File} {} dec2base (@var{n}, @var{b}) |
|
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 |
|
41 ## @end deftypefn |
|
42 ## |
|
43 ## @seealso{base2dec, dec2bin, bin2dec, hex2dec, dec2hex} |
|
44 |
3791
|
45 ## Author: Daniel Calvelo <dcalvelo@yahoo.com> |
3789
|
46 ## Adapted-by: Paul Kienzle <pkienzle@kienzle.powernet.co.uk> |
|
47 |
|
48 function out = dec2base (d, base) |
|
49 |
|
50 if (nargin != 2) |
|
51 usage("dec2base (n, base)"); |
|
52 endif |
|
53 |
|
54 if (prod (size (d)) != length (d)) |
|
55 error("dec2base: cannot convert matrices."); |
|
56 elseif (any (d < 0 | d != fix (d))) |
|
57 error("dec2base: can only convert non-negative integers.") |
|
58 endif |
|
59 |
|
60 symbols = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
|
61 if (isstr (base)) |
|
62 symbols = base; |
|
63 base = length (symbols); |
|
64 if any (diff (sort (toascii (symbols))) == 0) |
|
65 error ("dec2base: symbols representing digits must be unique."); |
|
66 endif |
|
67 elseif (! is_scalar (base)) |
|
68 error ("dec2base: cannot convert from several bases at once."); |
|
69 elseif (base < 2 || base > length (symbols)) |
|
70 error ("dec2base: base must be between 2 and 36 or a string of symbols"); |
|
71 endif |
|
72 |
|
73 ## determine number of digits required to handle all numbers |
|
74 maxLen = floor (log (max (max (d), 1)) ./ log (base)) + 1; |
|
75 |
|
76 ## determine digits for each number |
|
77 power = ones (length (d), 1) * (base .^ (maxLen-1 : -1 : 0)); |
|
78 d = d(:) * ones (1, maxLen); |
|
79 digits = floor (rem (d, base*power) ./ power); |
|
80 |
|
81 ## convert digits to symbols |
|
82 out = reshape (symbols (digits+1), size (digits)); |
|
83 |
|
84 endfunction |