2276
|
1 # Copyright (C) 1996 Kurt Hornik |
2268
|
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 the |
|
7 # Free Software Foundation; either version 2, or (at your option) any |
|
8 # later version. |
|
9 # |
|
10 # Octave is distributed in the hope that it will be useful, but WITHOUT |
|
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
12 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
13 # 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, write to the Free |
|
17 # Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
18 |
|
19 function h = dec2hex (d) |
|
20 |
|
21 # usage: dec2hex (d) |
|
22 # |
|
23 # Returns the hex number corresponding to the decimal number d. For |
|
24 # example, dec2hex (2748) returns "abc". |
|
25 |
|
26 # Original version by Kurt Hornik <Kurt.Hornik@ci.tuwien.ac.at>. |
|
27 |
|
28 if (nargin != 1) |
|
29 usage ("dec2hex (d)"); |
|
30 endif |
|
31 |
|
32 [nr, nc] = size (d); |
|
33 |
|
34 len = nr * nc; |
|
35 |
|
36 d = reshape (d, 1, len); |
|
37 |
|
38 eleo = empty_list_elements_ok; |
|
39 unwind_protect |
|
40 empty_list_elements_ok = 1; |
|
41 h = ""; |
|
42 for i = 1:len |
|
43 tmp = d (i); |
|
44 if (tmp == round (tmp)) |
|
45 h = [h, sprintf ("%x", tmp)]; |
|
46 else |
|
47 error ("dec2hex: invalid conversion"); |
|
48 endif |
|
49 endfor |
|
50 unwind_protect_cleanup |
|
51 empty_list_elements_ok = eleo; |
|
52 end_unwind_protect |
|
53 |
|
54 endfunction |