Mercurial > hg > octave-lyh
annotate scripts/strings/dec2hex.m @ 11172:7e8ce65f73cf
Overhaul functions used to convert between number bases.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Sun, 31 Oct 2010 07:30:15 -0700 |
parents | 693e22af08ae |
children | fd0a3ac60b0e |
rev | line source |
---|---|
9245 | 1 ## Copyright (C) 1996, 1999, 2000, 2001, 2003, 2005, 2006, 2007, 2008, 2009 |
7017 | 2 ## Daniel Calvelo |
2325 | 3 ## |
2313 | 4 ## This file is part of Octave. |
5 ## | |
6 ## Octave is free software; you can redistribute it and/or modify it | |
7 ## under the terms of the GNU General Public License as published by | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
2313 | 10 ## |
11 ## Octave is distributed in the hope that it will be useful, but | |
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 ## General Public License for more details. | |
15 ## | |
16 ## You should have received a copy of the GNU General Public License | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
2268 | 19 |
3361 | 20 ## -*- texinfo -*- |
11172
7e8ce65f73cf
Overhaul functions used to convert between number bases.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
21 ## @deftypefn {Function File} {} dec2hex (@var{d}, @var{len}) |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9051
diff
changeset
|
22 ## Return the hexadecimal string corresponding to the non-negative |
11172
7e8ce65f73cf
Overhaul functions used to convert between number bases.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
23 ## integer @var{d}. For example: |
3426 | 24 ## |
3361 | 25 ## @example |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
26 ## @group |
3361 | 27 ## dec2hex (2748) |
3789 | 28 ## @result{} "ABC" |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
29 ## @end group |
3361 | 30 ## @end example |
3789 | 31 ## |
11172
7e8ce65f73cf
Overhaul functions used to convert between number bases.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
32 ## If @var{d} is a vector, return a string matrix, one row per value, |
3789 | 33 ## padded with leading zeros to the width of the largest value. |
4492 | 34 ## |
35 ## The optional second argument, @var{len}, specifies the minimum | |
36 ## number of digits in the result. | |
11172
7e8ce65f73cf
Overhaul functions used to convert between number bases.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
37 ## @seealso{hex2dec, dec2base, dec2bin} |
3361 | 38 ## @end deftypefn |
2311 | 39 |
3791 | 40 ## Author: Daniel Calvelo <dcalvelo@yahoo.com> |
3789 | 41 ## Adapted-by: Paul Kienzle <pkienzle@kienzle.powernet.co.uk> |
2314 | 42 |
11172
7e8ce65f73cf
Overhaul functions used to convert between number bases.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
43 function h = dec2hex (d, len) |
2268 | 44 |
4492 | 45 if (nargin == 1) |
11172
7e8ce65f73cf
Overhaul functions used to convert between number bases.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
46 h = dec2base (d, 16); |
4492 | 47 elseif (nargin == 2) |
11172
7e8ce65f73cf
Overhaul functions used to convert between number bases.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
48 h = dec2base (d, 16, len); |
3789 | 49 else |
6046 | 50 print_usage (); |
2268 | 51 endif |
2325 | 52 |
2268 | 53 endfunction |
7411 | 54 |
11172
7e8ce65f73cf
Overhaul functions used to convert between number bases.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
55 %!assert(strcmpi (dec2hex (2748), "abc")); |
7e8ce65f73cf
Overhaul functions used to convert between number bases.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
56 %!assert(strcmpi (dec2hex (2748, 5), "00abc")); |
7411 | 57 |
11172
7e8ce65f73cf
Overhaul functions used to convert between number bases.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
58 %% Test input validation |
7411 | 59 %!error dec2hex (); |
60 %!error dec2hex (1, 2, 3); | |
61 |