annotate libinterp/corefcn/hex2num.cc @ 15195:2fc554ffbc28

split libinterp from src * libinterp: New directory. Move all files from src directory here except Makefile.am, main.cc, main-cli.cc, mkoctfile.in.cc, mkoctfilr.in.sh, octave-config.in.cc, octave-config.in.sh. * libinterp/Makefile.am: New file, extracted from src/Makefile.am. * src/Makefile.am: Delete everything except targets and definitions needed to build and link main and utility programs. * Makefile.am (SUBDIRS): Include libinterp in the list. * autogen.sh: Run config-module.sh in libinterp/dldfcn directory, not src/dldfcn directory. * configure.ac (AC_CONFIG_SRCDIR): Use libinterp/octave.cc, not src/octave.cc. (DL_LDFLAGS, LIBOCTINTERP): Use libinterp, not src. (AC_CONFIG_FILES): Include libinterp/Makefile in the list. * find-docstring-files.sh: Look in libinterp, not src. * gui/src/Makefile.am (liboctgui_la_CPPFLAGS): Find header files in libinterp, not src.
author John W. Eaton <jwe@octave.org>
date Sat, 18 Aug 2012 16:23:39 -0400
parents src/corefcn/hex2num.cc@e753177cde93
children 81d3c4409645
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
1 /*
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
2
14138
72c96de7a403 maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents: 13222
diff changeset
3 Copyright (C) 2008-2012 David Bateman
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
4
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
5 This file is part of Octave.
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
6
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
7 Octave is free software; you can redistribute it and/or modify it
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
8 under the terms of the GNU General Public License as published by the
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
9 Free Software Foundation; either version 3 of the License, or (at your
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
10 option) any later version.
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
11
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
12 Octave is distributed in the hope that it will be useful, but WITHOUT
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
15 for more details.
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
16
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
17 You should have received a copy of the GNU General Public License
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
18 along with Octave; see the file COPYING. If not, see
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
19 <http://www.gnu.org/licenses/>.
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
20
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
21 */
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
22
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
23 #ifdef HAVE_CONFIG_H
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
24 #include <config.h>
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
25 #endif
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
26
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
27 #include <algorithm>
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
28
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14501
diff changeset
29 #include "defun.h"
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
30 #include "error.h"
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
31 #include "gripes.h"
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
32 #include "oct-obj.h"
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
33 #include "utils.h"
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
34
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14501
diff changeset
35 DEFUN (hex2num, args, ,
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
36 "-*- texinfo -*-\n\
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14501
diff changeset
37 @deftypefn {Built-in Function} {@var{n} =} hex2num (@var{s})\n\
11553
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
38 Typecast the 16 character hexadecimal character string to an IEEE 754\n\
9036
58604c45ca74 Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
39 double precision number. If fewer than 16 characters are given the\n\
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
40 strings are right padded with '0' characters.\n\
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
41 \n\
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
42 Given a string matrix, @code{hex2num} treats each row as a separate\n\
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
43 number.\n\
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
44 \n\
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
45 @example\n\
9064
7c02ec148a3c Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents: 9036
diff changeset
46 @group\n\
14360
97883071e8e4 doc: Correct off-by-1 spacings in all .cc docstrings
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
47 hex2num ([\"4005bf0a8b145769\"; \"4024000000000000\"])\n\
97883071e8e4 doc: Correct off-by-1 spacings in all .cc docstrings
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
48 @result{} [2.7183; 10.000]\n\
9064
7c02ec148a3c Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents: 9036
diff changeset
49 @end group\n\
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
50 @end example\n\
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
51 @seealso{num2hex, hex2dec, dec2hex}\n\
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
52 @end deftypefn")
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
53 {
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
54 int nargin = args.length ();
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
55 octave_value retval;
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
56
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
57 if (nargin != 1)
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
58 print_usage ();
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
59 else
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
60 {
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
61 const charMatrix cmat = args(0).char_matrix_value ();
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
62
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
63 if (cmat.columns () > 16)
11553
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
64 error ("hex2num: S must be no more than 16 characters");
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
65 else if (! error_state)
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
66 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
67 octave_idx_type nr = cmat.rows ();
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
68 octave_idx_type nc = cmat.columns ();
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
69 ColumnVector m (nr);
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
70
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
71 for (octave_idx_type i = 0; i < nr; i++)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
72 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
73 union
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
74 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
75 uint64_t ival;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
76 double dval;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
77 } num;
8808
d724487d2c4b hex2num.cc: use union to avoid cast and GCC warning
John W. Eaton <jwe@octave.org>
parents: 8807
diff changeset
78
13222
82f3a0c27569 fix warnings for uninitialized variables
John W. Eaton <jwe@octave.org>
parents: 11586
diff changeset
79 num.ival = 0;
82f3a0c27569 fix warnings for uninitialized variables
John W. Eaton <jwe@octave.org>
parents: 11586
diff changeset
80
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
81 for (octave_idx_type j = 0; j < nc; j++)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
82 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
83 unsigned char ch = cmat.elem (i, j);
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
84
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
85 if (isxdigit (ch))
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
86 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
87 num.ival <<= 4;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
88 if (ch >= 'a')
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
89 num.ival += static_cast<uint64_t> (ch - 'a' + 10);
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
90 else if (ch >= 'A')
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
91 num.ival += static_cast<uint64_t> (ch - 'A' + 10);
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
92 else
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
93 num.ival += static_cast<uint64_t> (ch - '0');
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
94 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
95 else
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
96 {
11553
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
97 error ("hex2num: illegal character found in string S");
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
98 break;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
99 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
100 }
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
101
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
102 if (error_state)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
103 break;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
104 else
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
105 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
106 if (nc < 16)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
107 num.ival <<= (16 - nc) * 4;
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
108
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
109 m(i) = num.dval;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
110 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
111 }
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
112
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
113 if (! error_state)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
114 retval = m;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
115 }
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
116 }
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
117
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
118 return retval;
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
119 }
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
120
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
121 /*
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
122 %!assert (hex2num (["c00";"bff";"000";"3ff";"400"]), [-2:2]')
8808
d724487d2c4b hex2num.cc: use union to avoid cast and GCC warning
John W. Eaton <jwe@octave.org>
parents: 8807
diff changeset
123 */
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
124
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14501
diff changeset
125 DEFUN (num2hex, args, ,
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
126 "-*- texinfo -*-\n\
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14501
diff changeset
127 @deftypefn {Built-in Function} {@var{s} =} num2hex (@var{n})\n\
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
128 Typecast a double precision number or vector to a 16 character hexadecimal\n\
10840
89f4d7e294cc Grammarcheck .cc files
Rik <octave@nomad.inbox5.com>
parents: 10154
diff changeset
129 string of the IEEE 754 representation of the number. For example:\n\
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
130 \n\
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
131 @example\n\
9064
7c02ec148a3c Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents: 9036
diff changeset
132 @group\n\
14360
97883071e8e4 doc: Correct off-by-1 spacings in all .cc docstrings
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
133 num2hex ([-1, 1, e, Inf, NaN, NA])\n\
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
134 @result{} \"bff0000000000000\n\
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
135 3ff0000000000000\n\
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
136 4005bf0a8b145769\n\
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
137 7ff0000000000000\n\
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
138 fff8000000000000\n\
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
139 7ff00000000007a2\"\n\
9064
7c02ec148a3c Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents: 9036
diff changeset
140 @end group\n\
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
141 @end example\n\
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
142 @seealso{hex2num, hex2dec, dec2hex}\n\
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
143 @end deftypefn")
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
144 {
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
145 int nargin = args.length ();
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
146 octave_value retval;
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
147
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
148 if (nargin != 1)
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
149 print_usage ();
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
150 else
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
151 {
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
152 const ColumnVector v (args(0).vector_value ());
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
153
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
154 if (! error_state)
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
155 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
156 octave_idx_type nr = v.length ();
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
157 charMatrix m (nr, 16);
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
158 const double *pv = v.fortran_vec ();
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
159
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
160 for (octave_idx_type i = 0; i < nr; i++)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
161 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
162 union
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
163 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
164 uint64_t ival;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
165 double dval;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
166 } num;
8808
d724487d2c4b hex2num.cc: use union to avoid cast and GCC warning
John W. Eaton <jwe@octave.org>
parents: 8807
diff changeset
167
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
168 num.dval = *pv++;
8808
d724487d2c4b hex2num.cc: use union to avoid cast and GCC warning
John W. Eaton <jwe@octave.org>
parents: 8807
diff changeset
169
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
170 for (octave_idx_type j = 0; j < 16; j++)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
171 {
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11553
diff changeset
172 unsigned char ch =
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
173 static_cast<char> (num.ival >> ((15 - j) * 4) & 0xF);
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
174 if (ch >= 10)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
175 ch += 'a' - 10;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
176 else
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
177 ch += '0';
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
178
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
179 m.elem (i, j) = ch;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
180 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
181 }
9689
34d6f005db4b eliminate is_string argument from octave_value character array constructors
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
182
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
183 retval = m;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
184 }
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
185 }
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
186
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
187 return retval;
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
188 }
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
189
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
190 /*
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
191 %!assert (num2hex (-2:2), ["c000000000000000";"bff0000000000000";"0000000000000000";"3ff0000000000000";"4000000000000000"])
8808
d724487d2c4b hex2num.cc: use union to avoid cast and GCC warning
John W. Eaton <jwe@octave.org>
parents: 8807
diff changeset
192 */