]> Creatis software - CreaPhase.git/blob - octave_packages/m/strings/base2dec.m
update packages
[CreaPhase.git] / octave_packages / m / strings / base2dec.m
1 ## Copyright (C) 2000-2012 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 3 of the License, or (at
8 ## your option) 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, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
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
22 ## integer (base 10).
23 ##
24 ## @example
25 ## @group
26 ## base2dec ("11120", 3)
27 ##    @result{} 123
28 ## @end group
29 ## @end example
30 ##
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@.  
34 ##
35 ## If @var{s} is a cell array of strings, return a column vector with one
36 ## value per cell element in @var{s}.
37 ##
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
40 ## symbol.
41 ##
42 ## @example
43 ## @group
44 ## base2dec ("yyyzx", "xyz")
45 ##    @result{} 123
46 ## @end group
47 ## @end example
48 ## @seealso{dec2base, bin2dec, hex2dec}
49 ## @end deftypefn
50
51 ## Author: Daniel Calvelo <dcalvelo@yahoo.com>
52 ## Adapted-by: Paul Kienzle <pkienzle@kienzle.powernet.co.uk>
53
54 function out = base2dec (s, base)
55
56   if (nargin != 2)
57     print_usage ();
58   endif
59
60   if (iscellstr (s))
61     s = char (s);
62   elseif (! ischar (s))
63     error ("base2dec: S must be a string or cellstring");
64   endif
65
66   symbols = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
67   if (ischar (base))
68     symbols = base;
69     base = length (symbols);
70     if (length (unique (symbols)) != base)
71       error ("base2dec: symbols representing digits must be unique");
72     endif
73     if (any (isspace (symbols)))
74       error ("base2dec: whitespace characters are not valid symbols");
75     endif
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");
80   else
81     s = toupper (s);
82   endif
83
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.
87   [nr, nc] = size (s);
88   if (nc > 1)   # Bug #35621
89     s = s.';
90     nonbl = s != " ";
91     num_nonbl = sum (nonbl);
92     nc = max (num_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);
99     
100     ## Create a blank matrix and position the nonblank characters.
101     s2 = repmat (" ", nc, nr);
102     s2(idx) = s(nonbl);
103     s = s2.';
104   endif
105
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));
112
113   ## Multiply the resulting digits by the appropriate power
114   ## and sum the rows.
115   out = s * (base .^ (columns(s)-1 : -1 : 0)');
116
117 endfunction
118
119
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]);
124
125 %% Bug #35621
126 %!assert (base2dec (["0"; "1"], 2), [0; 1])
127
128 %%Test input validation
129 %!error base2dec ();
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);
136