Mercurial > hg > octave-nkf
annotate src/DLD-FUNCTIONS/hex2num.cc @ 9038:fca0dc2fb042
Cleanup documentation files stmt.texi and func.texi
Spellcheck
Style check (especially two spaces after period)
Info menu now uses @code macro when describing code statements such as while, for, if, etc.
author | Rik <rdrider0-list@yahoo.com> |
---|---|
date | Sun, 22 Mar 2009 11:15:35 -0700 |
parents | 58604c45ca74 |
children | 7c02ec148a3c |
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 |
8920 | 3 Copyright (C) 2008, 2009 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 |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
29 #include "defun-dld.h" |
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 |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
35 DEFUN_DLD (hex2num, args, , |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
36 "-*- texinfo -*-\n\ |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
37 @deftypefn {Loadable Function} {@var{n} =} hex2num (@var{s})\n\ |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
38 Typecast the 16 character hexadecimal character matrix to an IEEE 754\n\ |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
39 double precision number. If fewer than 16 characters are given the\n\ |
7639
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
40 strings are right padded with '0' characters.\n\ |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
41 \n\ |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
42 Given a string matrix, @code{hex2num} treats each row as a separate\n\ |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
43 number.\n\ |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
44 \n\ |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
45 @example\n\ |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
46 hex2num ([\"4005bf0a8b145769\";\"4024000000000000\"])\n\ |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
47 @result{} [2.7183; 10.000]\n\ |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
48 @end example\n\ |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
49 @seealso{num2hex, hex2dec, dec2hex}\n\ |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
50 @end deftypefn") |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
51 { |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
52 int nargin = args.length (); |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
53 octave_value retval; |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
54 |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
55 if (nargin != 1) |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
56 print_usage (); |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
57 else |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
58 { |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
59 const charMatrix cmat = args(0).char_matrix_value (); |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
60 |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
61 if (cmat.columns () > 16) |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
62 error ("hex2num: expecting no more than a 16 character string"); |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
63 else if (! error_state) |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
64 { |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
65 octave_idx_type nr = cmat.rows (); |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
66 octave_idx_type nc = cmat.columns (); |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
67 ColumnVector m (nr); |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
68 |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
69 for (octave_idx_type i = 0; i < nr; i++) |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
70 { |
8808
d724487d2c4b
hex2num.cc: use union to avoid cast and GCC warning
John W. Eaton <jwe@octave.org>
parents:
8807
diff
changeset
|
71 union |
d724487d2c4b
hex2num.cc: use union to avoid cast and GCC warning
John W. Eaton <jwe@octave.org>
parents:
8807
diff
changeset
|
72 { |
d724487d2c4b
hex2num.cc: use union to avoid cast and GCC warning
John W. Eaton <jwe@octave.org>
parents:
8807
diff
changeset
|
73 uint64_t ival; |
d724487d2c4b
hex2num.cc: use union to avoid cast and GCC warning
John W. Eaton <jwe@octave.org>
parents:
8807
diff
changeset
|
74 double dval; |
d724487d2c4b
hex2num.cc: use union to avoid cast and GCC warning
John W. Eaton <jwe@octave.org>
parents:
8807
diff
changeset
|
75 } num; |
d724487d2c4b
hex2num.cc: use union to avoid cast and GCC warning
John W. Eaton <jwe@octave.org>
parents:
8807
diff
changeset
|
76 |
7639
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
77 for (octave_idx_type j = 0; j < nc; j++) |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
78 { |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
79 unsigned char ch = cmat.elem (i, j); |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
80 |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
81 if (isxdigit (ch)) |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
82 { |
8808
d724487d2c4b
hex2num.cc: use union to avoid cast and GCC warning
John W. Eaton <jwe@octave.org>
parents:
8807
diff
changeset
|
83 num.ival <<= 4; |
7639
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
84 if (ch >= 'a') |
8808
d724487d2c4b
hex2num.cc: use union to avoid cast and GCC warning
John W. Eaton <jwe@octave.org>
parents:
8807
diff
changeset
|
85 num.ival += static_cast<uint64_t> (ch - 'a' + 10); |
7639
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
86 else if (ch >= 'A') |
8808
d724487d2c4b
hex2num.cc: use union to avoid cast and GCC warning
John W. Eaton <jwe@octave.org>
parents:
8807
diff
changeset
|
87 num.ival += static_cast<uint64_t> (ch - 'A' + 10); |
7639
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
88 else |
8808
d724487d2c4b
hex2num.cc: use union to avoid cast and GCC warning
John W. Eaton <jwe@octave.org>
parents:
8807
diff
changeset
|
89 num.ival += static_cast<uint64_t> (ch - '0'); |
7639
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
90 } |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
91 else |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
92 { |
8808
d724487d2c4b
hex2num.cc: use union to avoid cast and GCC warning
John W. Eaton <jwe@octave.org>
parents:
8807
diff
changeset
|
93 error ("hex2num: illegal character found in string"); |
7639
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
94 break; |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
95 } |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
96 } |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
97 |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
98 if (error_state) |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
99 break; |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
100 else |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
101 { |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
102 if (nc < 16) |
8808
d724487d2c4b
hex2num.cc: use union to avoid cast and GCC warning
John W. Eaton <jwe@octave.org>
parents:
8807
diff
changeset
|
103 num.ival <<= (16 - nc) * 4; |
7639
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
104 |
8808
d724487d2c4b
hex2num.cc: use union to avoid cast and GCC warning
John W. Eaton <jwe@octave.org>
parents:
8807
diff
changeset
|
105 m(i) = num.dval; |
7639
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
106 } |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
107 } |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
108 |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
109 if (! error_state) |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
110 retval = m; |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
111 } |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
112 } |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
113 |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
114 return retval; |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
115 } |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
116 |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
117 /* |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
118 %!assert (hex2num(['c00';'bff';'000';'3ff';'400']),[-2:2]') |
8808
d724487d2c4b
hex2num.cc: use union to avoid cast and GCC warning
John W. Eaton <jwe@octave.org>
parents:
8807
diff
changeset
|
119 */ |
7639
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
120 |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
121 DEFUN_DLD (num2hex, args, , |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
122 "-*- texinfo -*-\n\ |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
123 @deftypefn {Loadable Function} {@var{s} =} num2hex (@var{n})\n\ |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
124 Typecast a double precision number or vector to a 16 character hexadecimal\n\ |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
125 string of the IEEE 754 representation of the number. For example\n\ |
7639
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
126 \n\ |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
127 @example\n\ |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
128 num2hex ([-1, 1, e, Inf, NaN, NA]);\n\ |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
129 @result{} \"bff0000000000000\n\ |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
130 3ff0000000000000\n\ |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
131 4005bf0a8b145769\n\ |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
132 7ff0000000000000\n\ |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
133 fff8000000000000\n\ |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
134 7ff00000000007a2\"\n\ |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
135 @end example\n\ |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
136 @seealso{hex2num, hex2dec, dec2hex}\n\ |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
137 @end deftypefn") |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
138 { |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
139 int nargin = args.length (); |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
140 octave_value retval; |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
141 |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
142 if (nargin != 1) |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
143 print_usage (); |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
144 else |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
145 { |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
146 const ColumnVector v (args(0).vector_value ()); |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
147 |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
148 if (! error_state) |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
149 { |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
150 octave_idx_type nr = v.length (); |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
151 charMatrix m (nr, 16); |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
152 const double *pv = v.fortran_vec (); |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
153 |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
154 for (octave_idx_type i = 0; i < nr; i++) |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
155 { |
8808
d724487d2c4b
hex2num.cc: use union to avoid cast and GCC warning
John W. Eaton <jwe@octave.org>
parents:
8807
diff
changeset
|
156 union |
d724487d2c4b
hex2num.cc: use union to avoid cast and GCC warning
John W. Eaton <jwe@octave.org>
parents:
8807
diff
changeset
|
157 { |
d724487d2c4b
hex2num.cc: use union to avoid cast and GCC warning
John W. Eaton <jwe@octave.org>
parents:
8807
diff
changeset
|
158 uint64_t ival; |
d724487d2c4b
hex2num.cc: use union to avoid cast and GCC warning
John W. Eaton <jwe@octave.org>
parents:
8807
diff
changeset
|
159 double dval; |
d724487d2c4b
hex2num.cc: use union to avoid cast and GCC warning
John W. Eaton <jwe@octave.org>
parents:
8807
diff
changeset
|
160 } num; |
d724487d2c4b
hex2num.cc: use union to avoid cast and GCC warning
John W. Eaton <jwe@octave.org>
parents:
8807
diff
changeset
|
161 |
d724487d2c4b
hex2num.cc: use union to avoid cast and GCC warning
John W. Eaton <jwe@octave.org>
parents:
8807
diff
changeset
|
162 num.dval = *pv++; |
d724487d2c4b
hex2num.cc: use union to avoid cast and GCC warning
John W. Eaton <jwe@octave.org>
parents:
8807
diff
changeset
|
163 |
7639
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
164 for (octave_idx_type j = 0; j < 16; j++) |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
165 { |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
166 unsigned char ch = |
8808
d724487d2c4b
hex2num.cc: use union to avoid cast and GCC warning
John W. Eaton <jwe@octave.org>
parents:
8807
diff
changeset
|
167 static_cast<char> (num.ival >> ((15 - j) * 4) & 0xF); |
7639
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
168 if (ch >= 10) |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
169 ch += 'a' - 10; |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
170 else |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
171 ch += '0'; |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
172 |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
173 m.elem (i, j) = ch; |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
174 } |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
175 } |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
176 |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
177 retval = octave_value (m, true); |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
178 } |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
179 } |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
180 |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
181 return retval; |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
182 } |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
183 |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
184 /* |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
185 %!assert (num2hex (-2:2),['c000000000000000';'bff0000000000000';'0000000000000000';'3ff0000000000000';'4000000000000000']) |
8808
d724487d2c4b
hex2num.cc: use union to avoid cast and GCC warning
John W. Eaton <jwe@octave.org>
parents:
8807
diff
changeset
|
186 */ |