1 ## Copyright (C) 2000-2012 Daniel Calvelo
3 ## This file is part of Octave.
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 3 of the License, or (at
8 ## your option) any later version.
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.
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
20 ## @deftypefn {Function File} {} base2dec (@var{s}, @var{base})
21 ## Convert @var{s} from a string of digits in base @var{base} to a decimal
26 ## base2dec ("11120", 3)
31 ## If @var{s} is a string matrix, return a column vector with one value per
32 ## row of @var{s}. If a row contains invalid symbols then the
33 ## corresponding value will be NaN@.
35 ## If @var{s} is a cell array of strings, return a column vector with one
36 ## value per cell element in @var{s}.
38 ## If @var{base} is a string, the characters of @var{base} are used as the
39 ## symbols for the digits of @var{s}. Space (' ') may not be used as a
44 ## base2dec ("yyyzx", "xyz")
48 ## @seealso{dec2base, bin2dec, hex2dec}
51 ## Author: Daniel Calvelo <dcalvelo@yahoo.com>
52 ## Adapted-by: Paul Kienzle <pkienzle@kienzle.powernet.co.uk>
54 function out = base2dec (s, base)
63 error ("base2dec: S must be a string or cellstring");
66 symbols = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
69 base = length (symbols);
70 if (length (unique (symbols)) != base)
71 error ("base2dec: symbols representing digits must be unique");
73 if (any (isspace (symbols)))
74 error ("base2dec: whitespace characters are not valid symbols");
76 elseif (! isscalar (base))
77 error ("base2dec: cannot convert from several bases at once");
78 elseif (base < 2 || base > length (symbols))
79 error ("base2dec: BASE must be between 2 and 36, or a string of symbols");
84 ## Right justify the values and squeeze out any spaces.
85 ## This looks complicated, but indexing solution is very fast
86 ## compared to alternatives which use cellstr or cellfun or looping.
88 if (nc > 1) # Bug #35621
91 num_nonbl = sum (nonbl);
93 num_blank = nc - num_nonbl;
94 R = repmat ([1 2; 0 0], 1, nr);
95 R(2, 1:2:2*nr) = num_blank;
96 R(2, 2:2:2*nr) = num_nonbl;
97 idx = repelems ([false, true], R);
98 idx = reshape (idx, nc, nr);
100 ## Create a blank matrix and position the nonblank characters.
101 s2 = repmat (" ", nc, nr);
106 ## Lookup value of symbols in symbol table, with invalid symbols
107 ## evaluating to NaN and space evaluating to 0.
108 table = NaN (1, 256);
109 table(toascii (symbols(1:base))) = 0 : base-1;
110 table(toascii (" ")) = 0;
111 s = reshape (table(toascii (s)), size (s));
113 ## Multiply the resulting digits by the appropriate power
115 out = s * (base .^ (columns(s)-1 : -1 : 0)');
120 %!assert(base2dec ("11120", 3), 123);
121 %!assert(base2dec ("yyyzx", "xyz"), 123);
122 %!assert(base2dec ("-1", 2), NaN);
123 %!assert(base2dec ({"A1", "1A"}, 16), [161; 26]);
126 %!assert (base2dec (["0"; "1"], 2), [0; 1])
128 %%Test input validation
130 %!error base2dec ("11120");
131 %!error base2dec ("11120", 3, 4);
132 %!error base2dec ("11120", "1231");
133 %!error base2dec ("11120", "12 3");
134 %!error base2dec ("11120", ones(2));
135 %!error base2dec ("11120", 37);