]> Creatis software - CreaPhase.git/blob - octave_packages/m/strings/dec2hex.m
update packages
[CreaPhase.git] / octave_packages / m / strings / dec2hex.m
1 ## Copyright (C) 1996-2012 Daniel Calvelo
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
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) 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, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {} dec2hex (@var{d}, @var{len})
21 ## Return the hexadecimal string corresponding to the non-negative
22 ## integer @var{d}.  For example:
23 ##
24 ## @example
25 ## @group
26 ## dec2hex (2748)
27 ##      @result{} "ABC"
28 ## @end group
29 ## @end example
30 ##
31 ## If @var{d} is a matrix or cell array, return a string matrix with one
32 ## row per element in @var{d}, padded with leading zeros to the width of 
33 ## the largest value.
34 ##
35 ## The optional second argument, @var{len}, specifies the minimum
36 ## number of digits in the result.
37 ## @seealso{hex2dec, dec2base, dec2bin}
38 ## @end deftypefn
39
40 ## Author: Daniel Calvelo <dcalvelo@yahoo.com>
41 ## Adapted-by: Paul Kienzle <pkienzle@kienzle.powernet.co.uk>
42
43 function h = dec2hex (d, len)
44
45   if (nargin == 1)
46     h = dec2base (d, 16);
47   elseif (nargin == 2)
48     h = dec2base (d, 16, len);
49   else
50     print_usage ();
51   endif
52
53 endfunction
54
55
56 %!assert(dec2hex (2748), "ABC");
57 %!assert(dec2hex (2748, 5), "00ABC");
58 %!assert(dec2hex ({2748, 2746}), ["ABC"; "ABA"]);
59
60 %% Test input validation
61 %!error dec2hex ();
62 %!error dec2hex (1, 2, 3);
63