]> Creatis software - CreaPhase.git/blob - octave_packages/m/strings/hex2dec.m
update packages
[CreaPhase.git] / octave_packages / m / strings / hex2dec.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} {} hex2dec (@var{s})
21 ## Return the integer corresponding to the hexadecimal number represented
22 ## by the string @var{s}.  For example:
23 ##
24 ## @example
25 ## @group
26 ## hex2dec ("12B")
27 ##      @result{} 299
28 ## hex2dec ("12b")
29 ##      @result{} 299
30 ## @end group
31 ## @end example
32 ##
33 ## If @var{s} is a string matrix, return a column vector with one converted
34 ## number per row of @var{s}; Invalid rows evaluate to NaN@.
35 ##
36 ## If @var{s} is a cell array of strings, return a column vector with one
37 ## converted number per cell element in @var{s}.
38 ##
39 ## @seealso{dec2hex, base2dec, bin2dec}
40 ## @end deftypefn
41
42 ## Author: Daniel Calvelo <dcalvelo@yahoo.com>
43 ## Adapted-by: Paul Kienzle <pkienzle@kienzle.powernet.co.uk>
44
45 function d = hex2dec (s)
46
47   if (nargin != 1)
48     print_usage ();
49   endif
50
51   d = base2dec (s, 16);
52
53 endfunction
54
55
56 %!assert(hex2dec ("0000"), 0);
57 %!assert(hex2dec ("1FFFFFFFFFFFFF"), 2^53-1);
58 %!assert(hex2dec (["12b"; "12B"]), [299; 299]);
59 %!assert(hex2dec ({"A1", "1A"}), [161; 26]);
60
61 %%Test input validation
62 %!error hex2dec ();
63 %!error hex2dec (1);
64 %!error hex2dec ("1", 2);
65