7017
|
1 ## Copyright (C) 2000, 2001, 2002, 2004, 2005, 2006, 2007 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 -*- |
|
20 ## @deftypefn {Function File} {} base2dec (@var{s}, @var{b}) |
|
21 ## Convert @var{s} from a string of digits of base @var{b} into an |
|
22 ## integer. |
|
23 ## |
|
24 ## @example |
|
25 ## base2dec ("11120", 3) |
|
26 ## @result{} 123 |
|
27 ## @end example |
|
28 ## |
|
29 ## If @var{s} is a matrix, returns a column vector with one value per |
|
30 ## row of @var{s}. If a row contains invalid symbols then the |
|
31 ## corresponding value will be NaN. Rows are right-justified before |
|
32 ## converting so that trailing spaces are ignored. |
|
33 ## |
|
34 ## If @var{b} is a string, the characters of @var{b} are used as the |
|
35 ## symbols for the digits of @var{s}. Space (' ') may not be used as a |
|
36 ## symbol. |
|
37 ## |
|
38 ## @example |
|
39 ## base2dec ("yyyzx", "xyz") |
|
40 ## @result{} 123 |
|
41 ## @end example |
5642
|
42 ## @seealso{dec2base, dec2bin, bin2dec, hex2dec, dec2hex} |
3789
|
43 ## @end deftypefn |
|
44 |
3791
|
45 ## Author: Daniel Calvelo <dcalvelo@yahoo.com> |
3789
|
46 ## Adapted-by: Paul Kienzle <pkienzle@kienzle.powernet.co.uk> |
|
47 |
|
48 function out = base2dec (d, base) |
|
49 |
|
50 if (nargin != 2) |
6046
|
51 print_usage (); |
3789
|
52 endif |
|
53 |
|
54 symbols = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
5443
|
55 if (ischar (base)) |
3789
|
56 symbols = base; |
|
57 base = length (symbols); |
|
58 if (any (diff (sort (toascii (symbols))) == 0)) |
|
59 error ("base2dec: symbols representing digits must be unique."); |
|
60 endif |
4030
|
61 elseif (! isscalar (base)) |
3789
|
62 error ("base2dec: cannot convert from several bases at once."); |
|
63 elseif (base < 2 || base > length (symbols)) |
|
64 error ("base2dec: base must be between 2 and 36 or a string of symbols"); |
|
65 else |
|
66 d = toupper (d); |
|
67 endif |
|
68 |
|
69 ## Right justify the values before anything else. |
|
70 d = strjust (d, "right"); |
|
71 |
|
72 ## Lookup value of symbols in symbol table, with invalid symbols |
|
73 ## evaluating to NaN and space evaluating to 0. |
|
74 table = NaN * ones (256, 1); |
|
75 table (toascii (symbols (1 : base))) = 0 : base-1; |
|
76 table (toascii (" ")) = 0; |
|
77 d = reshape (table (toascii (d)), size (d)); |
|
78 |
|
79 ## Multiply the resulting digits by the appropriate power and |
|
80 ## sum the rows. |
|
81 out = d * (base .^ (columns(d)-1 : -1 : 0)'); |
|
82 |
|
83 endfunction |