changelog shortlog tags changeset files revisions annotate raw

src/DLD-FUNCTIONS/hex2num.cc

changeset 10289: 4b124317dc38
parent:34d6f005db4b
author: John W. Eaton <jwe@octave.org>
date: Tue Feb 09 20:58:55 2010 -0500 (56 minutes ago)
permissions: -rw-r--r--
description: base_properties::set_children: account for hidden children
1/*
2
3Copyright (C) 2008, 2009 David Bateman
4
5This file is part of Octave.
6
7Octave is free software; you can redistribute it and/or modify it
8under the terms of the GNU General Public License as published by the
9Free Software Foundation; either version 3 of the License, or (at your
10option) any later version.
11
12Octave is distributed in the hope that it will be useful, but WITHOUT
13ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License
18along with Octave; see the file COPYING. If not, see
19<http://www.gnu.org/licenses/>.
20
21*/
22
23#ifdef HAVE_CONFIG_H
24#include <config.h>
25#endif
26
27#include <algorithm>
28
29#include "defun-dld.h"
30#include "error.h"
31#include "gripes.h"
32#include "oct-obj.h"
33#include "utils.h"
34
35DEFUN_DLD (hex2num, args, ,
36 "-*- texinfo -*-\n\
37@deftypefn {Loadable Function} {@var{n} =} hex2num (@var{s})\n\
38Typecast the 16 character hexadecimal character matrix to an IEEE 754\n\
39double precision number. If fewer than 16 characters are given the\n\
40strings are right padded with '0' characters.\n\
41\n\
42Given a string matrix, @code{hex2num} treats each row as a separate\n\
43number.\n\
44\n\
45@example\n\
46@group\n\
47hex2num ([\"4005bf0a8b145769\";\"4024000000000000\"])\n\
48@result{} [2.7183; 10.000]\n\
49@end group\n\
50@end example\n\
51@seealso{num2hex, hex2dec, dec2hex}\n\
52@end deftypefn")
53{
54 int nargin = args.length ();
55 octave_value retval;
56
57 if (nargin != 1)
58 print_usage ();
59 else
60 {
61 const charMatrix cmat = args(0).char_matrix_value ();
62
63 if (cmat.columns () > 16)
64 error ("hex2num: expecting no more than a 16 character string");
65 else if (! error_state)
66 {
67 octave_idx_type nr = cmat.rows ();
68 octave_idx_type nc = cmat.columns ();
69 ColumnVector m (nr);
70
71 for (octave_idx_type i = 0; i < nr; i++)
72 {
73 union
74 {
75 uint64_t ival;
76 double dval;
77 } num;
78
79 for (octave_idx_type j = 0; j < nc; j++)
80 {
81 unsigned char ch = cmat.elem (i, j);
82
83 if (isxdigit (ch))
84 {
85 num.ival <<= 4;
86 if (ch >= 'a')
87 num.ival += static_cast<uint64_t> (ch - 'a' + 10);
88 else if (ch >= 'A')
89 num.ival += static_cast<uint64_t> (ch - 'A' + 10);
90 else
91 num.ival += static_cast<uint64_t> (ch - '0');
92 }
93 else
94 {
95 error ("hex2num: illegal character found in string");
96 break;
97 }
98 }
99
100 if (error_state)
101 break;
102 else
103 {
104 if (nc < 16)
105 num.ival <<= (16 - nc) * 4;
106
107 m(i) = num.dval;
108 }
109 }
110
111 if (! error_state)
112 retval = m;
113 }
114 }
115
116 return retval;
117}
118
119/*
120%!assert (hex2num(['c00';'bff';'000';'3ff';'400']),[-2:2]')
121*/
122
123DEFUN_DLD (num2hex, args, ,
124 "-*- texinfo -*-\n\
125@deftypefn {Loadable Function} {@var{s} =} num2hex (@var{n})\n\
126Typecast a double precision number or vector to a 16 character hexadecimal\n\
127string of the IEEE 754 representation of the number. For example\n\
128\n\
129@example\n\
130@group\n\
131num2hex ([-1, 1, e, Inf, NaN, NA]);\n\
132@result{} \"bff0000000000000\n\
133 3ff0000000000000\n\
134 4005bf0a8b145769\n\
135 7ff0000000000000\n\
136 fff8000000000000\n\
137 7ff00000000007a2\"\n\
138@end group\n\
139@end example\n\
140@seealso{hex2num, hex2dec, dec2hex}\n\
141@end deftypefn")
142{
143 int nargin = args.length ();
144 octave_value retval;
145
146 if (nargin != 1)
147 print_usage ();
148 else
149 {
150 const ColumnVector v (args(0).vector_value ());
151
152 if (! error_state)
153 {
154 octave_idx_type nr = v.length ();
155 charMatrix m (nr, 16);
156 const double *pv = v.fortran_vec ();
157
158 for (octave_idx_type i = 0; i < nr; i++)
159 {
160 union
161 {
162 uint64_t ival;
163 double dval;
164 } num;
165
166 num.dval = *pv++;
167
168 for (octave_idx_type j = 0; j < 16; j++)
169 {
170 unsigned char ch =
171 static_cast<char> (num.ival >> ((15 - j) * 4) & 0xF);
172 if (ch >= 10)
173 ch += 'a' - 10;
174 else
175 ch += '0';
176
177 m.elem (i, j) = ch;
178 }
179 }
180
181 retval = m;
182 }
183 }
184
185 return retval;
186}
187
188/*
189%!assert (num2hex (-2:2),['c000000000000000';'bff0000000000000';'0000000000000000';'3ff0000000000000';'4000000000000000'])
190*/