Mercurial > hg > octave-nkf
comparison scripts/strings/bin2dec.m @ 14491:5bd9e47e9277
maint: periodic merge of stable to default
author | Jordi Gutiérrez Hermoso <jordigh@octave.org> |
---|---|
date | Thu, 22 Mar 2012 11:50:51 -0400 |
parents | cbcaf5602469 e995b1c97e13 |
children | 5d3a684236b0 |
comparison
equal
deleted
inserted
replaced
14489:f36301ea650f | 14491:5bd9e47e9277 |
---|---|
22 ## by the string @var{s}. For example: | 22 ## by the string @var{s}. For example: |
23 ## | 23 ## |
24 ## @example | 24 ## @example |
25 ## @group | 25 ## @group |
26 ## bin2dec ("1110") | 26 ## bin2dec ("1110") |
27 ## @result{} 14 | 27 ## @result{} 14 |
28 ## @end group | |
29 ## @end example | |
30 ## | |
31 ## Spaces are ignored during conversion and may be used to make the binary | |
32 ## number more readable. | |
33 ## | |
34 ## @example | |
35 ## @group | |
36 ## bin2dec ("1000 0001") | |
37 ## @result{} 129 | |
28 ## @end group | 38 ## @end group |
29 ## @end example | 39 ## @end example |
30 ## | 40 ## |
31 ## If @var{s} is a string matrix, return a column vector with one converted | 41 ## If @var{s} is a string matrix, return a column vector with one converted |
32 ## number per row of @var{s}; Invalid rows evaluate to NaN@. | 42 ## number per row of @var{s}; Invalid rows evaluate to NaN@. |
48 d = base2dec (s, 2); | 58 d = base2dec (s, 2); |
49 | 59 |
50 endfunction | 60 endfunction |
51 | 61 |
52 | 62 |
53 %!assert (bin2dec ("0000"), 0) | 63 %!assert(bin2dec ("0000"), 0) |
54 %!assert (bin2dec ("1110"), 14) | 64 %!assert(bin2dec ("1110"), 14) |
55 %!assert (bin2dec ("11111111111111111111111111111111111111111111111111111"), 2^53-1) | 65 %!assert(bin2dec ("11111111111111111111111111111111111111111111111111111"), 2^53-1) |
56 %!assert (bin2dec ({"1110", "1111"}), [14; 15]) | 66 %!assert(bin2dec ({"1110", "1111"}), [14; 15]) |
67 %!assert (bin2dec ("1 0 1"), 5) | |
68 %!assert (bin2dec (char ("1 0 1", " 1111")), [5; 15]) | |
57 | 69 |
58 %%Test input validation | 70 %%Test input validation |
59 %!error bin2dec () | 71 %!error bin2dec () |
60 %!error bin2dec (1) | 72 %!error bin2dec (1) |
61 %!error bin2dec ("1", 2) | 73 %!error bin2dec ("1", 2) |