Mercurial > octave
annotate src/DLD-FUNCTIONS/hex2num.cc @ 7639:b2fbb393a072
Add the num2hex and hex2num functions
author | David Bateman <dbateman@free.fr> |
---|---|
date | Tue, 25 Mar 2008 20:22:46 -0400 |
parents | |
children | 401d54a83690 |
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 |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
3 Copyright (C) 2008 David Bateman |
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\ |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
39 double precision number. If fewer than 16 characters are given the\n\ |
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 { |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
71 uint64_t num = 0; |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
72 for (octave_idx_type j = 0; j < nc; j++) |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
73 { |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
74 unsigned char ch = cmat.elem (i, j); |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
75 |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
76 if (isxdigit (ch)) |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
77 { |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
78 num <<= 4; |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
79 if (ch >= 'a') |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
80 num += static_cast<uint64_t> (ch - 'a' + 10); |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
81 else if (ch >= 'A') |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
82 num += static_cast<uint64_t> (ch - 'A' + 10); |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
83 else |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
84 num += static_cast<uint64_t> (ch - '0'); |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
85 } |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
86 else |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
87 { |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
88 error ("hex2num: illegal character found in string"); |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
89 break; |
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 } |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
92 |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
93 if (error_state) |
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 else |
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 if (nc < 16) |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
98 num <<= (16 - nc) * 4; |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
99 |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
100 m (i) = *reinterpret_cast<double *>(&num); |
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 } |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
103 } |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
104 |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
105 if (! error_state) |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
106 retval = m; |
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 |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
110 return retval; |
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 |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
115 %!assert (hex2num(['c00';'bff';'000';'3ff';'400']),[-2:2]') |
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 |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
119 |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
120 |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
121 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\ |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
125 string of the IEEE 754 representation of the number. For example\n\ |
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 { |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
156 const uint64_t num = *reinterpret_cast<const uint64_t *> (pv++); |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
157 for (octave_idx_type j = 0; j < 16; j++) |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
158 { |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
159 unsigned char ch = |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
160 static_cast<char> (num >> ((15 - j) * 4) & 0xF); |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
161 if (ch >= 10) |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
162 ch += 'a' - 10; |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
163 else |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
164 ch += '0'; |
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 m.elem (i, j) = ch; |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
167 } |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
168 } |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
169 |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
170 retval = octave_value (m, true); |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
171 } |
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 |
b2fbb393a072
Add the num2hex and hex2num functions
David Bateman <dbateman@free.fr>
parents:
diff
changeset
|
174 return retval; |
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 /* |
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 %!assert (num2hex (-2:2),['c000000000000000';'bff0000000000000';'0000000000000000';'3ff0000000000000';'4000000000000000']) |
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 */ |
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 |