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 |
5307
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
3789
|
19 |
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} base2dec (@var{s}, @var{b}) |
|
22 ## Convert @var{s} from a string of digits of base @var{b} into an |
|
23 ## integer. |
|
24 ## |
|
25 ## @example |
|
26 ## base2dec ("11120", 3) |
|
27 ## @result{} 123 |
|
28 ## @end example |
|
29 ## |
|
30 ## If @var{s} is a matrix, returns a column vector with one value per |
|
31 ## row of @var{s}. If a row contains invalid symbols then the |
|
32 ## corresponding value will be NaN. Rows are right-justified before |
|
33 ## converting so that trailing spaces are ignored. |
|
34 ## |
|
35 ## If @var{b} is a string, the characters of @var{b} are used as the |
|
36 ## symbols for the digits of @var{s}. Space (' ') may not be used as a |
|
37 ## symbol. |
|
38 ## |
|
39 ## @example |
|
40 ## base2dec ("yyyzx", "xyz") |
|
41 ## @result{} 123 |
|
42 ## @end example |
|
43 ## |
|
44 ## @end deftypefn |
5053
|
45 ## |
3789
|
46 ## @seealso{dec2base, 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 |
|
51 function out = base2dec (d, base) |
|
52 |
|
53 if (nargin != 2) |
|
54 usage ("n = base2dec('d', base)"); |
|
55 endif |
|
56 |
|
57 symbols = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
|
58 if (isstr (base)) |
|
59 symbols = base; |
|
60 base = length (symbols); |
|
61 if (any (diff (sort (toascii (symbols))) == 0)) |
|
62 error ("base2dec: symbols representing digits must be unique."); |
|
63 endif |
4030
|
64 elseif (! isscalar (base)) |
3789
|
65 error ("base2dec: cannot convert from several bases at once."); |
|
66 elseif (base < 2 || base > length (symbols)) |
|
67 error ("base2dec: base must be between 2 and 36 or a string of symbols"); |
|
68 else |
|
69 d = toupper (d); |
|
70 endif |
|
71 |
|
72 ## Right justify the values before anything else. |
|
73 d = strjust (d, "right"); |
|
74 |
|
75 ## Lookup value of symbols in symbol table, with invalid symbols |
|
76 ## evaluating to NaN and space evaluating to 0. |
|
77 table = NaN * ones (256, 1); |
|
78 table (toascii (symbols (1 : base))) = 0 : base-1; |
|
79 table (toascii (" ")) = 0; |
|
80 d = reshape (table (toascii (d)), size (d)); |
|
81 |
|
82 ## Multiply the resulting digits by the appropriate power and |
|
83 ## sum the rows. |
|
84 out = d * (base .^ (columns(d)-1 : -1 : 0)'); |
|
85 |
|
86 endfunction |