3
Copyright (C) 2008 David Bateman
5
This program is free software; you can redistribute it and/or modify it
6
under the terms of the GNU General Public License as published by the
7
Free Software Foundation; either version 3 of the License, or (at your
8
option) any later version.
10
This program is distributed in the hope that it will be useful, but WITHOUT
11
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15
You should have received a copy of the GNU General Public License
16
along with this program; see the file COPYING. If not, see
17
<http://www.gnu.org/licenses/>.
24
#include <octave/defun-dld.h>
25
#include <octave/error.h>
26
#include <octave/gripes.h>
27
#include <octave/oct-obj.h>
28
#include <octave/utils.h>
30
DEFUN_DLD (hex2num, args, ,
32
@deftypefn {Loadable Function} {@var{n} =} hex2num (@var{s})\n\
33
Typecast the 16 character hexadecimal character matrix to an IEEE 754\n\
34
double precision number. If fewer than 16 characters are given the\n\
35
strings are right padded with '0' characters.\n\
37
Given a string matrix, @code{hex2num} treats each row as a separate\n\
41
hex2num ([\"4005bf0a8b145769\";\"4024000000000000\"])\n\
42
@result{} [2.7183; 10.000]\n\
44
@seealso{num2hex, hex2dec, dec2hex}\n\
47
int nargin = args.length ();
54
const charMatrix cmat = args(0).char_matrix_value ();
56
if (cmat.columns () > 16)
57
error ("hex2num: expecting no more than a 16 character string");
58
else if (! error_state)
60
octave_idx_type nr = cmat.rows ();
61
octave_idx_type nc = cmat.columns ();
64
for (octave_idx_type i = 0; i < nr; i++)
67
for (octave_idx_type j = 0; j < nc; j++)
69
unsigned char ch = cmat.elem (i, j);
75
num += static_cast<uint64_t> (ch - 'a' + 10);
77
num += static_cast<uint64_t> (ch - 'A' + 10);
79
num += static_cast<uint64_t> (ch - '0');
83
error ("hex2num: illegal character found in string");
93
num <<= (16 - nc) * 4;
95
m (i) = *reinterpret_cast<double *>(&num);
110
%!assert (hex2num(['c00';'bff';'000';'3ff';'400']),[-2:2]')
115
// PKG_ADD: autoload ("num2hex", "hex2num.oct");
116
DEFUN_DLD (num2hex, args, ,
118
@deftypefn {Loadable Function} {@var{s} =} num2hex (@var{n})\n\
119
Typecast a double precision number or vector to a 16 character hexadecimal\n\
120
string of the IEEE 754 representation of the number. For example\n\
123
num2hex ([-1, 1, e, Inf, NaN, NA]);\n\
124
@result{} \"bff0000000000000\n\
129
7ff00000000007a2\"\n\
131
@seealso{hex2num, hex2dec, dec2hex}\n\
134
int nargin = args.length ();
141
const ColumnVector v (args(0).vector_value ());
145
octave_idx_type nr = v.length ();
146
charMatrix m (nr, 16);
147
const double *pv = v.fortran_vec ();
149
for (octave_idx_type i = 0; i < nr; i++)
151
const uint64_t num = *reinterpret_cast<const uint64_t *> (pv++);
152
for (octave_idx_type j = 0; j < 16; j++)
155
static_cast<char> (num >> ((15 - j) * 4) & 0xF);
165
retval = octave_value (m, true);
174
%!assert (num2hex (-2:2),['c000000000000000';'bff0000000000000';'0000000000000000';'3ff0000000000000';'4000000000000000'])