1########################################################################
2##
3## Copyright (C) 1996-2021 The Octave Project Developers
4##
5## See the file COPYRIGHT.md in the top-level directory of this
6## distribution or <https://octave.org/copyright/>.
7##
8## This file is part of Octave.
9##
10## Octave is free software: you can redistribute it and/or modify it
11## under the terms of the GNU General Public License as published by
12## the Free Software Foundation, either version 3 of the License, or
13## (at your option) any later version.
14##
15## Octave is distributed in the hope that it will be useful, but
16## WITHOUT ANY WARRANTY; without even the implied warranty of
17## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18## GNU General Public License for more details.
19##
20## You should have received a copy of the GNU General Public License
21## along with Octave; see the file COPYING.  If not, see
22## <https://www.gnu.org/licenses/>.
23##
24########################################################################
25
26## -*- texinfo -*-
27## @deftypefn {} {} dec2bin (@var{d}, @var{len})
28## Return a binary number corresponding to the non-negative integer @var{d},
29## as a string of ones and zeros.
30##
31## For example:
32##
33## @example
34## @group
35## dec2bin (14)
36##      @result{} "1110"
37## @end group
38## @end example
39##
40## If @var{d} is a matrix or cell array, return a string matrix with one row
41## per element in @var{d}, padded with leading zeros to the width of the
42## largest value.
43##
44## The optional second argument, @var{len}, specifies the minimum number of
45## digits in the result.
46## @seealso{bin2dec, dec2base, dec2hex}
47## @end deftypefn
48
49function b = dec2bin (d, len)
50
51  if (nargin == 1)
52    b = dec2base (d, 2);
53  elseif (nargin == 2)
54    b = dec2base (d, 2, len);
55  else
56    print_usage ();
57  endif
58
59endfunction
60
61
62%!assert (dec2bin (14), "1110")
63%!assert (dec2bin (14, 6), "001110")
64%!assert (dec2bin ({1, 2; 3, 4}), ["001"; "011"; "010"; "100"])
65
66## Test input validation
67%!error dec2bin ()
68%!error dec2bin (1, 2, 3)
69