3789
|
1 ## Copyright (C) 2001 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 |
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## 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, write to the Free |
5307
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
2268
|
19 |
3361
|
20 ## -*- texinfo -*- |
4492
|
21 ## @deftypefn {Function File} {} dec2bin (@var{n}, @var{len}) |
3361
|
22 ## Return a binary number corresponding the nonnegative decimal number |
|
23 ## @var{n}, as a string of ones and zeros. For example, |
3426
|
24 ## |
3361
|
25 ## @example |
|
26 ## dec2bin (14) |
|
27 ## @result{} "1110" |
|
28 ## @end example |
3789
|
29 ## |
|
30 ## If @var{n} is a vector, returns a string matrix, one row per value, |
|
31 ## padded with leading zeros to the width of the largest value. |
4492
|
32 ## |
|
33 ## The optional second argument, @var{len}, specifies the minimum |
|
34 ## number of digits in the result. |
3361
|
35 ## @end deftypefn |
3789
|
36 ## |
|
37 ## @seealso{bin2dec, dec2base, base2dec, hex2dec, dec2hex} |
2268
|
38 |
3791
|
39 ## Author: Daniel Calvelo <dcalvelo@yahoo.com> |
4492
|
40 ## Adapted-by: Paul Kienzle <pkienzle@kienzle.powernet.co.uk> |
2314
|
41 |
4492
|
42 function retval = dec2bin (n, len) |
2268
|
43 |
4492
|
44 if (nargin == 1) |
|
45 retval = dec2base (n, 2); |
|
46 elseif (nargin == 2) |
|
47 retval = dec2base (n, 2, len); |
3789
|
48 else |
4492
|
49 usage ("dec2bin (n [, len])"); |
2268
|
50 endif |
|
51 |
|
52 endfunction |