Mercurial > hg > octave-nkf
annotate scripts/strings/base2dec.m @ 9051:1bf0ce0930be
Grammar check TexInfo in all .m files
Cleanup documentation sources to follow a few consistent rules.
Spellcheck was NOT done. (but will be in another changeset)
author | Rik <rdrider0-list@yahoo.com> |
---|---|
date | Fri, 27 Mar 2009 22:31:03 -0700 |
parents | 58604c45ca74 |
children | 952d4df5b686 |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2009 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 | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
9036
diff
changeset
|
25 ## @group |
3789 | 26 ## base2dec ("11120", 3) |
27 ## @result{} 123 | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
9036
diff
changeset
|
28 ## @end group |
3789 | 29 ## @end example |
30 ## | |
31 ## If @var{s} is a matrix, returns 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. Rows are right-justified before | |
34 ## converting so that trailing spaces are ignored. | |
35 ## | |
36 ## If @var{b} is a string, the characters of @var{b} are used as the | |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
37 ## symbols for the digits of @var{s}. Space (' ') may not be used as a |
3789 | 38 ## symbol. |
39 ## | |
40 ## @example | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
9036
diff
changeset
|
41 ## @group |
3789 | 42 ## base2dec ("yyyzx", "xyz") |
43 ## @result{} 123 | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
9036
diff
changeset
|
44 ## @end group |
3789 | 45 ## @end example |
5642 | 46 ## @seealso{dec2base, dec2bin, bin2dec, hex2dec, dec2hex} |
3789 | 47 ## @end deftypefn |
48 | |
3791 | 49 ## Author: Daniel Calvelo <dcalvelo@yahoo.com> |
3789 | 50 ## Adapted-by: Paul Kienzle <pkienzle@kienzle.powernet.co.uk> |
51 | |
52 function out = base2dec (d, base) | |
53 | |
54 if (nargin != 2) | |
6046 | 55 print_usage (); |
3789 | 56 endif |
57 | |
58 symbols = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
5443 | 59 if (ischar (base)) |
3789 | 60 symbols = base; |
61 base = length (symbols); | |
62 if (any (diff (sort (toascii (symbols))) == 0)) | |
8664 | 63 error ("base2dec: symbols representing digits must be unique"); |
3789 | 64 endif |
4030 | 65 elseif (! isscalar (base)) |
8664 | 66 error ("base2dec: cannot convert from several bases at once"); |
3789 | 67 elseif (base < 2 || base > length (symbols)) |
68 error ("base2dec: base must be between 2 and 36 or a string of symbols"); | |
69 else | |
70 d = toupper (d); | |
71 endif | |
72 | |
73 ## Right justify the values before anything else. | |
74 d = strjust (d, "right"); | |
75 | |
76 ## Lookup value of symbols in symbol table, with invalid symbols | |
77 ## evaluating to NaN and space evaluating to 0. | |
78 table = NaN * ones (256, 1); | |
79 table (toascii (symbols (1 : base))) = 0 : base-1; | |
80 table (toascii (" ")) = 0; | |
81 d = reshape (table (toascii (d)), size (d)); | |
82 | |
83 ## Multiply the resulting digits by the appropriate power and | |
84 ## sum the rows. | |
85 out = d * (base .^ (columns(d)-1 : -1 : 0)'); | |
86 | |
87 endfunction | |
8442
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
88 |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
89 %!error <Invalid call to base2dec.*> base2dec(); |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
90 %!error <Invalid call to base2dec.*> base2dec("11120"); |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
91 %!error <Invalid call to base2dec.*> base2dec("11120", 3, 4); |
502e58a0d44f
Fix docstrings, add examples, references and tests to string functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
7017
diff
changeset
|
92 %!assert(base2dec ("11120", 3), 123); |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
93 %!assert(base2dec ("yyyzx", "xyz"), 123); |