Mercurial > hg > octave-lyh
annotate scripts/strings/bin2dec.m @ 11172:7e8ce65f73cf
Overhaul functions used to convert between number bases.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Sun, 31 Oct 2010 07:30:15 -0700 |
parents | 693e22af08ae |
children | fd0a3ac60b0e |
rev | line source |
---|---|
9245 | 1 ## Copyright (C) 1996, 2000, 2001, 2005, 2006, 2007, 2008, 2009 |
2 ## Daniel Calvelo | |
2325 | 3 ## |
2313 | 4 ## This file is part of Octave. |
5 ## | |
6 ## Octave is free software; you can redistribute it and/or modify it | |
7 ## under the terms of the GNU General Public License as published by | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
2313 | 10 ## |
11 ## Octave is distributed in the hope that it will be useful, but | |
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 ## General Public License for more details. | |
15 ## | |
16 ## You should have received a copy of the GNU General Public License | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
2268 | 19 |
3446 | 20 ## -*- texinfo -*- |
5666 | 21 ## @deftypefn {Function File} {} bin2dec (@var{s}) |
11172
7e8ce65f73cf
Overhaul functions used to convert between number bases.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
22 ## Return the decimal number corresponding to the binary number represented |
7e8ce65f73cf
Overhaul functions used to convert between number bases.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
23 ## by the string @var{s}. For example: |
2311 | 24 ## |
3446 | 25 ## @example |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
26 ## @group |
5666 | 27 ## bin2dec ("1110") |
3446 | 28 ## @result{} 14 |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
29 ## @end group |
3446 | 30 ## @end example |
3789 | 31 ## |
11172
7e8ce65f73cf
Overhaul functions used to convert between number bases.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
32 ## If @var{s} is a string matrix, return a column vector of converted |
3789 | 33 ## numbers, one per row of @var{s}. Invalid rows evaluate to NaN. |
11172
7e8ce65f73cf
Overhaul functions used to convert between number bases.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
34 ## @seealso{dec2bin, base2dec, hex2dec} |
3446 | 35 ## @end deftypefn |
2311 | 36 |
3791 | 37 ## Author: Daniel Calvelo <dcalvelo@yahoo.com> |
3789 | 38 ## Adapted-by: Paul Kienzle <pkienzle@kienzle.powernet.co.uk> |
2314 | 39 |
11172
7e8ce65f73cf
Overhaul functions used to convert between number bases.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
40 function d = bin2dec (s) |
2268 | 41 |
11172
7e8ce65f73cf
Overhaul functions used to convert between number bases.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
42 if (nargin == 1 && ischar (s)) |
7e8ce65f73cf
Overhaul functions used to convert between number bases.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
43 d = base2dec (s, 2); |
2268 | 44 else |
5924 | 45 print_usage (); |
2268 | 46 endif |
2325 | 47 |
2268 | 48 endfunction |
7411 | 49 |
11172
7e8ce65f73cf
Overhaul functions used to convert between number bases.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
50 %!assert(bin2dec ("0000"), 0); |
7e8ce65f73cf
Overhaul functions used to convert between number bases.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
51 %!assert(bin2dec ("1110"), 14); |
7e8ce65f73cf
Overhaul functions used to convert between number bases.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
52 %!assert(bin2dec ("11111111111111111111111111111111111111111111111111111"), 2^53-1); |
7411 | 53 |
11172
7e8ce65f73cf
Overhaul functions used to convert between number bases.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
54 %%Test input validation |
7411 | 55 %!error bin2dec (); |
11172
7e8ce65f73cf
Overhaul functions used to convert between number bases.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
56 %!error bin2dec (1); |
7e8ce65f73cf
Overhaul functions used to convert between number bases.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
57 %!error bin2dec ("1", 2); |
7411 | 58 |