7017
|
1 ## Copyright (C) 1996, 2000, 2001, 2005, 2006, 2007 Daniel Calvelo |
2325
|
2 ## |
2313
|
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. |
2313
|
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/>. |
2268
|
18 |
3446
|
19 ## -*- texinfo -*- |
5666
|
20 ## @deftypefn {Function File} {} bin2dec (@var{s}) |
3789
|
21 ## Return the decimal number corresponding to the binary number stored |
|
22 ## in the string @var{s}. For example, |
2311
|
23 ## |
3446
|
24 ## @example |
5666
|
25 ## bin2dec ("1110") |
3446
|
26 ## @result{} 14 |
|
27 ## @end example |
3789
|
28 ## |
|
29 ## If @var{s} is a string matrix, returns a column vector of converted |
|
30 ## numbers, one per row of @var{s}. Invalid rows evaluate to NaN. |
5666
|
31 ## @seealso{dec2hex, base2dec, dec2base, hex2dec, dec2bin} |
3446
|
32 ## @end deftypefn |
2311
|
33 |
3791
|
34 ## Author: Daniel Calvelo <dcalvelo@yahoo.com> |
3789
|
35 ## Adapted-by: Paul Kienzle <pkienzle@kienzle.powernet.co.uk> |
2314
|
36 |
3789
|
37 function d = bin2dec (h) |
2268
|
38 |
5924
|
39 if (nargin == 1 && ischar (h)) |
|
40 n = rows (h); |
|
41 d = zeros (n, 1); |
|
42 for i = 1:n |
|
43 s = h(i,:); |
|
44 s = s(! isspace (s)); |
|
45 d(i) = base2dec (s, 2); |
|
46 endfor |
2268
|
47 else |
5924
|
48 print_usage (); |
2268
|
49 endif |
2325
|
50 |
2268
|
51 endfunction |
7411
|
52 |
|
53 %!assert(bin2dec ("1110") == 14); |
|
54 |
|
55 %!error bin2dec (); |
|
56 |
|
57 %!error bin2dec ("str", 1); |
|
58 |