annotate libinterp/corefcn/hex2num.cc @ 20232:a9574e3c6e9e

Deprecate Array::length() and Sparse::length() in favour of ::numel(). * liboctave/array/Array.h (Array::length): deprecate for ::numel. * liboctave/array/Sparse.h (Sparse::length): deprecate for ::numel. * libgui/graphics/QtHandlesUtils.cc, libgui/src/octave-qt-link.cc, libinterp/corefcn/Cell.cc, libinterp/corefcn/__contourc__.cc, libinterp/corefcn/__pchip_deriv__.cc, libinterp/corefcn/__qp__.cc, libinterp/corefcn/cellfun.cc, libinterp/corefcn/daspk.cc, libinterp/corefcn/dasrt.cc, libinterp/corefcn/dassl.cc, libinterp/corefcn/data.cc, libinterp/corefcn/filter.cc, libinterp/corefcn/find.cc, libinterp/corefcn/gl-render.cc, libinterp/corefcn/gl-render.h, libinterp/corefcn/graphics.cc, libinterp/corefcn/graphics.in.h, libinterp/corefcn/help.cc, libinterp/corefcn/hex2num.cc, libinterp/corefcn/input.cc, libinterp/corefcn/load-path.cc, libinterp/corefcn/load-save.cc, libinterp/corefcn/ls-oct-ascii.h, libinterp/corefcn/lsode.cc, libinterp/corefcn/matrix_type.cc, libinterp/corefcn/oct-hist.cc, libinterp/corefcn/oct-map.cc, libinterp/corefcn/oct-map.h, libinterp/corefcn/oct-obj.h, libinterp/corefcn/oct-stream.cc, libinterp/corefcn/oct-stream.h, libinterp/corefcn/pr-output.cc, libinterp/corefcn/quadcc.cc, libinterp/corefcn/rand.cc, libinterp/corefcn/regexp.cc, libinterp/corefcn/strfns.cc, libinterp/corefcn/sub2ind.cc, libinterp/corefcn/symtab.h, libinterp/corefcn/syscalls.cc, libinterp/corefcn/tsearch.cc, libinterp/corefcn/urlwrite.cc, libinterp/corefcn/utils.cc, libinterp/corefcn/variables.cc, libinterp/corefcn/xdiv.cc, libinterp/corefcn/xpow.cc, libinterp/dldfcn/__glpk__.cc, libinterp/dldfcn/__init_fltk__.cc, libinterp/dldfcn/__magick_read__.cc, libinterp/dldfcn/audiodevinfo.cc, libinterp/dldfcn/ccolamd.cc, libinterp/dldfcn/colamd.cc, libinterp/octave-value/ov-cell.cc, libinterp/octave-value/ov-class.cc, libinterp/octave-value/ov-class.h, libinterp/octave-value/ov-fcn-inline.cc, libinterp/octave-value/ov-java.cc, libinterp/octave-value/ov-perm.cc, libinterp/octave-value/ov-struct.cc, libinterp/octave-value/ov-typeinfo.cc, libinterp/octave-value/ov.cc, libinterp/operators/op-int.h, libinterp/parse-tree/pt-pr-code.cc, liboctave/array/Array-util.cc, liboctave/array/Array.cc, liboctave/array/CColVector.cc, liboctave/array/CDiagMatrix.cc, liboctave/array/CMatrix.cc, liboctave/array/CRowVector.cc, liboctave/array/DiagArray2.cc, liboctave/array/DiagArray2.h, liboctave/array/MArray.cc, liboctave/array/PermMatrix.cc, liboctave/array/PermMatrix.h, liboctave/array/Sparse.cc, liboctave/array/boolMatrix.cc, liboctave/array/chMatrix.cc, liboctave/array/chNDArray.cc, liboctave/array/dColVector.cc, liboctave/array/dDiagMatrix.cc, liboctave/array/dMatrix.cc, liboctave/array/dRowVector.cc, liboctave/array/fCColVector.cc, liboctave/array/fCDiagMatrix.cc, liboctave/array/fCMatrix.cc, liboctave/array/fCRowVector.cc, liboctave/array/fColVector.cc, liboctave/array/fDiagMatrix.cc, liboctave/array/fMatrix.cc, liboctave/array/fRowVector.cc, liboctave/array/idx-vector.cc, liboctave/array/intNDArray.cc, liboctave/numeric/CmplxCHOL.cc, liboctave/numeric/CmplxLU.cc, liboctave/numeric/CmplxQR.cc, liboctave/numeric/DASPK.cc, liboctave/numeric/DASRT.cc, liboctave/numeric/DASSL.cc, liboctave/numeric/LSODE.cc, liboctave/numeric/ODES.cc, liboctave/numeric/ODES.h, liboctave/numeric/base-dae.h, liboctave/numeric/base-lu.cc, liboctave/numeric/dbleCHOL.cc, liboctave/numeric/dbleLU.cc, liboctave/numeric/dbleQR.cc, liboctave/numeric/eigs-base.cc, liboctave/numeric/fCmplxCHOL.cc, liboctave/numeric/fCmplxLU.cc, liboctave/numeric/fCmplxQR.cc, liboctave/numeric/floatCHOL.cc, liboctave/numeric/floatLU.cc, liboctave/numeric/floatQR.cc, liboctave/numeric/lo-specfun.cc, liboctave/numeric/oct-rand.cc, liboctave/numeric/oct-spparms.cc, liboctave/numeric/sparse-base-chol.cc, liboctave/operators/mx-inlines.cc, liboctave/system/file-ops.cc, liboctave/util/glob-match.h, liboctave/util/kpse.cc, liboctave/util/lo-regexp.cc, liboctave/util/oct-glob.cc, liboctave/util/pathsearch.cc, liboctave/util/str-vec.cc, liboctave/util/str-vec.h, liboctave/util/url-transfer.cc: replace all usage of Array::length() and Sparse::length() with ::numel().
author Carnë Draug <carandraug@octave.org>
date Sat, 30 May 2015 03:14:07 +0100
parents 4f45eaf83908
children 4b00afb5e9c3
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
19697
4197fc428c7d maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents: 17787
diff changeset
3 Copyright (C) 2008-2015 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, ,
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
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\
20172
4f45eaf83908 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19697
diff changeset
40 double precision number.\n\
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
41 \n\
20172
4f45eaf83908 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19697
diff changeset
42 If fewer than 16 characters are given the strings are right padded with\n\
4f45eaf83908 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19697
diff changeset
43 @qcode{'0'} characters.\n\
4f45eaf83908 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19697
diff changeset
44 \n\
4f45eaf83908 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19697
diff changeset
45 Given a string matrix, @code{hex2num} treats each row as a separate number.\n\
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
46 \n\
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
47 @example\n\
9064
7c02ec148a3c Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents: 9036
diff changeset
48 @group\n\
14360
97883071e8e4 doc: Correct off-by-1 spacings in all .cc docstrings
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
49 hex2num ([\"4005bf0a8b145769\"; \"4024000000000000\"])\n\
97883071e8e4 doc: Correct off-by-1 spacings in all .cc docstrings
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
50 @result{} [2.7183; 10.000]\n\
9064
7c02ec148a3c Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents: 9036
diff changeset
51 @end group\n\
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
52 @end example\n\
17178
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
53 \n\
17281
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 17178
diff changeset
54 The optional argument @var{class} can be passed as the string\n\
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 17178
diff changeset
55 @qcode{\"single\"} to specify that the given string should be interpreted as\n\
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 17178
diff changeset
56 a single precision number. In this case, @var{s} should be an 8 character\n\
20172
4f45eaf83908 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19697
diff changeset
57 hexadecimal string. For example:\n\
17281
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 17178
diff changeset
58 \n\
17178
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
59 @example\n\
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
60 @group\n\
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
61 hex2num ([\"402df854\"; \"41200000\"], \"single\")\n\
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
62 @result{} [2.7183; 10.000]\n\
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
63 @end group\n\
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
64 @end example\n\
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
65 @seealso{num2hex, hex2dec, dec2hex}\n\
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
66 @end deftypefn")
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
67 {
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
68 int nargin = args.length ();
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
69 octave_value retval;
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
70
17178
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
71 if (nargin < 1 || nargin > 2)
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
72 print_usage ();
17178
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
73 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
74 error ("hex2num: CLASS must be a string");
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
75 else
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
76 {
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
77 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
78 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
79 bool is_single = (prec == "single");
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
80 octave_idx_type nchars = (is_single) ? 8 : 16;
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
81
17178
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
82 if (cmat.columns () > nchars)
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
83 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
84 else if (prec != "double" && prec != "single")
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
85 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
86 else if (! error_state)
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
87 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
88 octave_idx_type nr = cmat.rows ();
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
89 octave_idx_type nc = cmat.columns ();
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
90
17178
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
91 if (is_single)
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
92 {
17178
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
93 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
94
17178
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
95 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
96 {
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
97 union
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
98 {
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
99 uint32_t ival;
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
100 float dval;
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
101 } num;
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 num.ival = 0;
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 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
106 {
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
107 unsigned char ch = cmat.elem (i, j);
13222
82f3a0c27569 fix warnings for uninitialized variables
John W. Eaton <jwe@octave.org>
parents: 11586
diff changeset
108
17178
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
109 if (isxdigit (ch))
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
110 {
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
111 num.ival <<= 4;
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
112 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 if (ch >= 'A')
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 - 'A' + 10);
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
116 else
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
117 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
118 }
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
119 else
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
120 {
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
121 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
122 break;
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
123 }
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
124 }
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
125
17178
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
126 if (error_state)
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
127 break;
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
128 else
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
129 {
17178
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
130 if (nc < nchars)
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
131 num.ival <<= (nchars - nc) * 4;
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
132
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
133 m(i) = num.dval;
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
134 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
135 }
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
136
17178
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
137 if (! error_state)
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
138 retval = m;
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 else
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 ColumnVector m (nr);
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
143
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
144 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
145 {
17178
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
146 union
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
147 {
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
148 uint64_t ival;
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
149 double dval;
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
150 } num;
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 num.ival = 0;
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 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
155 {
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
156 unsigned char ch = cmat.elem (i, j);
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
157
17178
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
158 if (isxdigit (ch))
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
159 {
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
160 num.ival <<= 4;
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
161 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 if (ch >= 'A')
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 - 'A' + 10);
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
165 else
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
166 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
167 }
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
168 else
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
169 {
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
170 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
171 break;
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 }
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
174
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
175 if (error_state)
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
176 break;
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
177 else
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
178 {
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
179 if (nc < nchars)
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
180 num.ival <<= (nchars - nc) * 4;
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
181
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
182 m(i) = num.dval;
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
183 }
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
184 }
17178
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
185
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
186 if (! error_state)
f010db8b710c hex2num: Add single precision conversion option (bug #39608)
Mike Miller <mtmiller@ieee.org>
parents: 17177
diff changeset
187 retval = m;
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
188 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
189 }
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
190 }
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 return retval;
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
193 }
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
194
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
195 /*
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
196 %!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
197 %!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
198 */
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
199
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14501
diff changeset
200 DEFUN (num2hex, args, ,
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
201 "-*- texinfo -*-\n\
15039
e753177cde93 maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents: 14501
diff changeset
202 @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
203 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
204 character hexadecimal string of the IEEE 754 representation of the number.\n\
20172
4f45eaf83908 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19697
diff changeset
205 \n\
17177
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
206 For example:\n\
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
207 \n\
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
208 @example\n\
9064
7c02ec148a3c Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents: 9036
diff changeset
209 @group\n\
17177
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
210 num2hex ([-1, 1, e, Inf])\n\
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
211 @result{} \"bff0000000000000\n\
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
212 3ff0000000000000\n\
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
213 4005bf0a8b145769\n\
17177
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
214 7ff0000000000000\"\n\
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
215 @end group\n\
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
216 @end 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 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
219 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
220 \n\
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
221 @example\n\
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
222 @group\n\
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
223 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
224 @result{} \"bf800000\n\
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
225 3f800000\n\
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
226 402df854\n\
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
227 7f800000\"\n\
9064
7c02ec148a3c Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents: 9036
diff changeset
228 @end group\n\
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
229 @end example\n\
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
230 @seealso{hex2num, hex2dec, dec2hex}\n\
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
231 @end deftypefn")
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 int nargin = args.length ();
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
234 octave_value retval;
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
235
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
236 if (nargin != 1)
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
237 print_usage ();
17177
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
238 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
239 {
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
240 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
241
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
242 if (! error_state)
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
243 {
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
244 octave_idx_type nchars = 8;
20232
a9574e3c6e9e Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents: 20172
diff changeset
245 octave_idx_type nr = v.numel ();
17177
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
246 charMatrix m (nr, nchars);
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
247 const float *pv = v.fortran_vec ();
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
248
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
249 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
250 {
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
251 union
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
252 {
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
253 uint32_t ival;
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
254 float dval;
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
255 } num;
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
256
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
257 num.dval = *pv++;
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
258
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
259 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
260 {
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
261 unsigned char ch =
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
262 static_cast<char>(num.ival >> ((nchars - 1 - j) * 4) & 0xF);
17177
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
263 if (ch >= 10)
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
264 ch += 'a' - 10;
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
265 else
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
266 ch += '0';
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 m.elem (i, j) = ch;
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
269 }
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
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
272 retval = m;
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
273 }
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
274 }
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
275 else
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
276 {
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
277 const ColumnVector v (args(0).vector_value ());
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
278
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
279 if (! error_state)
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
280 {
17177
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
281 octave_idx_type nchars = 16;
20232
a9574e3c6e9e Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents: 20172
diff changeset
282 octave_idx_type nr = v.numel ();
17177
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
283 charMatrix m (nr, nchars);
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
284 const double *pv = v.fortran_vec ();
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
285
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
286 for (octave_idx_type i = 0; i < nr; i++)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
287 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
288 union
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
289 {
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
290 uint64_t ival;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
291 double dval;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
292 } num;
8808
d724487d2c4b hex2num.cc: use union to avoid cast and GCC warning
John W. Eaton <jwe@octave.org>
parents: 8807
diff changeset
293
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
294 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
295
17177
81d3c4409645 num2hex: Handle single precision argument correctly (bug #39607)
Mike Miller <mtmiller@ieee.org>
parents: 15195
diff changeset
296 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
297 {
11586
12df7854fa7c strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents: 11553
diff changeset
298 unsigned char ch =
17787
175b392e91fe Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents: 17744
diff changeset
299 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
300 if (ch >= 10)
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
301 ch += 'a' - 10;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
302 else
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
303 ch += '0';
7639
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
304
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
305 m.elem (i, j) = ch;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
306 }
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
307 }
9689
34d6f005db4b eliminate is_string argument from octave_value character array constructors
John W. Eaton <jwe@octave.org>
parents: 9064
diff changeset
308
10154
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
309 retval = m;
40dfc0c99116 DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents: 9689
diff changeset
310 }
7639
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 return retval;
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
314 }
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
315
b2fbb393a072 Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff changeset
316 /*
14501
60e5cf354d80 Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents: 14360
diff changeset
317 %!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
318 %!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
319 */