comparison scripts/strings/dec2bin.m @ 3789:2a257be5e488

[project @ 2001-02-09 04:12:30 by jwe]
author jwe
date Fri, 09 Feb 2001 04:12:31 +0000
parents f8dde1807dee
children c1c532a0acb2
comparison
equal deleted inserted replaced
3788:c60b54937cfe 3789:2a257be5e488
1 ## Copyright (C) 1996 Kurt Hornik 1 ## Copyright (C) 2001 Daniel Calvelo
2 ## 2 ##
3 ## This file is part of Octave. 3 ## This file is part of Octave.
4 ## 4 ##
5 ## Octave is free software; you can redistribute it and/or modify it 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 6 ## under the terms of the GNU General Public License as published by
24 ## 24 ##
25 ## @example 25 ## @example
26 ## dec2bin (14) 26 ## dec2bin (14)
27 ## @result{} "1110" 27 ## @result{} "1110"
28 ## @end example 28 ## @end example
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.
29 ## @end deftypefn 32 ## @end deftypefn
33 ##
34 ## @seealso{bin2dec, dec2base, base2dec, hex2dec, dec2hex}
30 35
31 ## Author: Kurt Hornik <Kurt.Hornik@ci.tuwien.ac.at> 36 ## Author: Daniel Calvelo
32 ## Adapted-By: jwe 37 ## 2001-02-02 Paul Kienzle <pkienzle@kienzle.powernet.co.uk>
33 38
34 function y = dec2bin (x) 39 function h = dec2bin (d)
35 40
36 if (nargin != 1) 41 if (nargin != 1)
37 usage ("dec2bin (x)"); 42 usage ("dec2bin (b)");
43 else
44 h = dec2base (d, 2);
38 endif 45 endif
39 46
40 [nr, nc] = size (x);
41
42 len = nr * nc;
43
44 x = reshape (x, 1, len);
45
46 eleo = empty_list_elements_ok;
47 unwind_protect
48 empty_list_elements_ok = 1;
49 y = [];
50 for i = 1:len
51 tmp = x (i);
52 if (tmp == round (tmp) && tmp >= 0)
53 while (tmp >= 2)
54 z = fix (tmp ./ 2);
55 y = [y, tmp - 2 * z];
56 tmp = z;
57 endwhile
58 y = [y, tmp];
59 else
60 error ("dec2hex: invalid conversion");
61 endif
62 endfor
63 y = fliplr (y);
64 y = setstr (y + toascii ("0"));
65 unwind_protect_cleanup
66 empty_list_elements_ok = eleo;
67 end_unwind_protect
68
69 endfunction 47 endfunction