annotate libinterp/corefcn/hex2num.cc @ 17178:f010db8b710c

hex2num: Add single precision conversion option (bug #39608) * hex2num.cc (Fhex2num): Add optional argument to allow conversion of single precision strings while remaining Matlab-compatible. Update docstring to describe the new argument and behavior. Add test case.
author Mike Miller <mtmiller@ieee.org>
date Sun, 04 Aug 2013 20:35:55 -0400
parents 81d3c4409645
children bc924baa2c4e
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\
17178
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
37 @deftypefn {Built-in Function} {@var{n} =} hex2num (@var{s})\n\
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
38 @deftypefnx {Built-in Function} {@var{n} =} hex2num (@var{s}, @var{class})\n\
11553
01f703952eff Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents: 11523
diff changeset
39 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
40 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
41 strings are right padded with '0' characters.\n\
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
42 \n\
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
43 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
44 number.\n\
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
45 \n\
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
46 @example\n\
9064
7c02ec148a3c Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents: 9036
diff changeset
47 @group\n\
14360
97883071e8e4 doc: Correct off-by-1 spacings in all .cc docstrings
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
48 hex2num ([\"4005bf0a8b145769\"; \"4024000000000000\"])\n\
97883071e8e4 doc: Correct off-by-1 spacings in all .cc docstrings
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
49 @result{} [2.7183; 10.000]\n\
9064
7c02ec148a3c Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents: 9036
diff changeset
50 @end group\n\
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
51 @end example\n\
17178
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
52 \n\
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
53 The optional argument @var{class} can be passed as the string \"single\" to\n\
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
54 specify that the given string should be interpreted as a single precision\n\
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
55 number. In this case, @var{s} should be an 8 character hexadecimal string.\n\
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
56 For example:\n\
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
57 @example\n\
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
58 @group\n\
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
59 hex2num ([\"402df854\"; \"41200000\"], \"single\")\n\
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
60 @result{} [2.7183; 10.000]\n\
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
61 @end group\n\
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
62 @end example\n\
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
63 @seealso{num2hex, hex2dec, dec2hex}\n\
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
64 @end deftypefn")
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
65 {
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
66 int nargin = args.length ();
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
67 octave_value retval;
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
68
17178
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
69 if (nargin < 1 || nargin > 2)
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
70 print_usage ();
17178
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
71 else if (nargin == 2 && ! args(1).is_string ())
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
72 error ("hex2num: CLASS must be a string");
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
73 else
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
74 {
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
75 const charMatrix cmat = args(0).char_matrix_value ();
17178
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
76 std::string prec = (nargin == 2) ? args(1).string_value () : "double";
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
77 bool is_single = (prec == "single");
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
78 octave_idx_type nchars = (is_single) ? 8 : 16;
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
79
17178
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
80 if (cmat.columns () > nchars)
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
81 error ("hex2num: S must be no more than %d characters", nchars);
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
82 else if (prec != "double" && prec != "single")
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
83 error ("hex2num: CLASS must be either \"double\" or \"single\"");
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
84 else if (! error_state)
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
85 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
86 octave_idx_type nr = cmat.rows ();
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
87 octave_idx_type nc = cmat.columns ();
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
88
17178
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
89 if (is_single)
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
90 {
17178
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
91 FloatColumnVector m (nr);
8808
d724487d2c4b hex2num.cc: use union to avoid cast and GCC warning
John W. Eaton <jwe@octave.org>
parents: 8807
diff changeset
92
17178
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
93 for (octave_idx_type i = 0; i < nr; i++)
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
94 {
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
95 union
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
96 {
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
97 uint32_t ival;
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
98 float dval;
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
99 } num;
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
100
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
101 num.ival = 0;
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
102
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
103 for (octave_idx_type j = 0; j < nc; j++)
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
104 {
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
105 unsigned char ch = cmat.elem (i, j);
13222
82f3a0c27569 fix warnings for uninitialized variables
John W. Eaton <jwe@octave.org>
parents: 11586
diff changeset
106
17178
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
107 if (isxdigit (ch))
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
108 {
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
109 num.ival <<= 4;
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
110 if (ch >= 'a')
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
111 num.ival += static_cast<uint32_t> (ch - 'a' + 10);
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
112 else if (ch >= 'A')
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
113 num.ival += static_cast<uint32_t> (ch - 'A' + 10);
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
114 else
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
115 num.ival += static_cast<uint32_t> (ch - '0');
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
116 }
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
117 else
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
118 {
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
119 error ("hex2num: illegal character found in string S");
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
120 break;
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
121 }
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
122 }
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
123
17178
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
124 if (error_state)
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
125 break;
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
126 else
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
127 {
17178
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
128 if (nc < nchars)
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
129 num.ival <<= (nchars - nc) * 4;
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
130
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
131 m(i) = num.dval;
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
132 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
133 }
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
134
17178
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
135 if (! error_state)
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
136 retval = m;
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
137 }
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
138 else
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
139 {
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
140 ColumnVector m (nr);
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
141
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
142 for (octave_idx_type i = 0; i < nr; i++)
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
143 {
17178
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
144 union
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
145 {
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
146 uint64_t ival;
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
147 double dval;
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
148 } num;
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
149
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
150 num.ival = 0;
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
151
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
152 for (octave_idx_type j = 0; j < nc; j++)
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
153 {
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
154 unsigned char ch = cmat.elem (i, j);
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
155
17178
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
156 if (isxdigit (ch))
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
157 {
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
158 num.ival <<= 4;
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
159 if (ch >= 'a')
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
160 num.ival += static_cast<uint64_t> (ch - 'a' + 10);
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
161 else if (ch >= 'A')
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
162 num.ival += static_cast<uint64_t> (ch - 'A' + 10);
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
163 else
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
164 num.ival += static_cast<uint64_t> (ch - '0');
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
165 }
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
166 else
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
167 {
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
168 error ("hex2num: illegal character found in string S");
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
169 break;
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
170 }
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
171 }
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
172
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
173 if (error_state)
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
174 break;
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
175 else
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
176 {
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
177 if (nc < nchars)
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
178 num.ival <<= (nchars - nc) * 4;
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
179
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
180 m(i) = num.dval;
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
181 }
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
182 }
17178
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
183
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
184 if (! error_state)
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
185 retval = m;
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
186 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
187 }
7639
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 return retval;
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
191 }
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
192
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
193 /*
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
194 %!assert (hex2num (["c00";"bff";"000";"3ff";"400"]), [-2:2]')
17178
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
195 %!assert (hex2num (["c00";"bf8";"000";"3f8";"400"], "single"), single([-2:2])')
8808
d724487d2c4b hex2num.cc: use union to avoid cast and GCC warning
John W. Eaton <jwe@octave.org>
parents: 8807
diff changeset
196 */
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
197
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14501
diff changeset
198 DEFUN (num2hex, args, ,
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
199 "-*- texinfo -*-\n\
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14501
diff changeset
200 @deftypefn {Built-in Function} {@var{s} =} num2hex (@var{n})\n\
17177
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
201 Typecast a double or single precision number or vector to a 8 or 16\n\
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
202 character hexadecimal string of the IEEE 754 representation of the number.\n\
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
203 For example:\n\
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
204 \n\
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
205 @example\n\
9064
7c02ec148a3c Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents: 9036
diff changeset
206 @group\n\
17177
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
207 num2hex ([-1, 1, e, Inf])\n\
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
208 @result{} \"bff0000000000000\n\
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
209 3ff0000000000000\n\
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
210 4005bf0a8b145769\n\
17177
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
211 7ff0000000000000\"\n\
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
212 @end group\n\
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
213 @end example\n\
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
214 \n\
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
215 If the argument @var{n} is a single precision number or vector, the returned\n\
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
216 string has a length of 8. For example:\n\
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
217 \n\
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
218 @example\n\
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
219 @group\n\
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
220 num2hex (single ([-1, 1, e, Inf]))\n\
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
221 @result{} \"bf800000\n\
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
222 3f800000\n\
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
223 402df854\n\
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
224 7f800000\"\n\
9064
7c02ec148a3c Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents: 9036
diff changeset
225 @end group\n\
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
226 @end example\n\
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
227 @seealso{hex2num, hex2dec, dec2hex}\n\
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
228 @end deftypefn")
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
229 {
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
230 int nargin = args.length ();
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
231 octave_value retval;
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
232
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
233 if (nargin != 1)
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
234 print_usage ();
17177
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
235 else if (args(0).is_single_type ())
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
236 {
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
237 const FloatColumnVector v (args(0).float_vector_value ());
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
238
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
239 if (! error_state)
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
240 {
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
241 octave_idx_type nchars = 8;
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
242 octave_idx_type nr = v.length ();
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
243 charMatrix m (nr, nchars);
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
244 const float *pv = v.fortran_vec ();
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
245
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
246 for (octave_idx_type i = 0; i < nr; i++)
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
247 {
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
248 union
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
249 {
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
250 uint32_t ival;
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
251 float dval;
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
252 } num;
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
253
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
254 num.dval = *pv++;
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
255
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
256 for (octave_idx_type j = 0; j < nchars; j++)
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
257 {
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
258 unsigned char ch =
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
259 static_cast<char> (num.ival >> ((nchars - 1 - j) * 4) & 0xF);
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
260 if (ch >= 10)
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
261 ch += 'a' - 10;
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
262 else
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
263 ch += '0';
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
264
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
265 m.elem (i, j) = ch;
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
266 }
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
267 }
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
268
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
269 retval = m;
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
270 }
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
271 }
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
272 else
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
273 {
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
274 const ColumnVector v (args(0).vector_value ());
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
275
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
276 if (! error_state)
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
277 {
17177
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
278 octave_idx_type nchars = 16;
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
279 octave_idx_type nr = v.length ();
17177
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
280 charMatrix m (nr, nchars);
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
281 const double *pv = v.fortran_vec ();
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
282
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
283 for (octave_idx_type i = 0; i < nr; i++)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
284 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
285 union
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
286 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
287 uint64_t ival;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
288 double dval;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
289 } num;
8808
d724487d2c4b hex2num.cc: use union to avoid cast and GCC warning
John W. Eaton <jwe@octave.org>
parents: 8807
diff changeset
290
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
291 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
292
17177
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
293 for (octave_idx_type j = 0; j < nchars; j++)
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
294 {
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11553
diff changeset
295 unsigned char ch =
17177
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
296 static_cast<char> (num.ival >> ((nchars - 1 - j) * 4) & 0xF);
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
297 if (ch >= 10)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
298 ch += 'a' - 10;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
299 else
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
300 ch += '0';
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
301
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
302 m.elem (i, j) = ch;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
303 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
304 }
9689
34d6f005db4b eliminate is_string argument from octave_value character array constructors
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
305
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
306 retval = m;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
307 }
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
308 }
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
309
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
310 return retval;
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
311 }
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
312
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
313 /*
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
314 %!assert (num2hex (-2:2), ["c000000000000000";"bff0000000000000";"0000000000000000";"3ff0000000000000";"4000000000000000"])
17177
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
315 %!assert (num2hex (single (-2:2)), ["c0000000";"bf800000";"00000000";"3f800000";"40000000"])
8808
d724487d2c4b hex2num.cc: use union to avoid cast and GCC warning
John W. Eaton <jwe@octave.org>
parents: 8807
diff changeset
316 */