~ubuntu-branches/ubuntu/wily/octave-miscellaneous/wily-proposed

« back to all changes in this revision

Viewing changes to src/hex2num.cc

  • Committer: Bazaar Package Importer
  • Author(s): Thomas Weber
  • Date: 2011-05-12 20:57:43 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20110512205743-ej2yjlpcbtbrwdjo
Tags: 1.0.11-1
* New upstream release (Closes: #626264)
* debian/control:
  - Remove Rafael Laboissiere from Uploaders (Closes: #571851)
  - Remove Ólafur Jens Sigurðsson <ojsbug@gmail.com> from Uploaders 
* Bump Standards-Version to 3.9.1, no changes needed
* Switch to dpkg-source 3.0 (quilt) format

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 
3
 
Copyright (C) 2008 David Bateman
4
 
 
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.
9
 
 
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
13
 
for more details.
14
 
 
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/>.
18
 
 
19
 
*/
20
 
 
21
 
#include <config.h>
22
 
#include <algorithm>
23
 
 
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>
29
 
 
30
 
DEFUN_DLD (hex2num, args, ,
31
 
  "-*- texinfo -*-\n\
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\
36
 
\n\
37
 
Given a string matrix, @code{hex2num} treats each row as a separate\n\
38
 
number.\n\
39
 
\n\
40
 
@example\n\
41
 
hex2num ([\"4005bf0a8b145769\";\"4024000000000000\"])\n\
42
 
@result{} [2.7183; 10.000]\n\
43
 
@end example\n\
44
 
@seealso{num2hex, hex2dec, dec2hex}\n\
45
 
@end deftypefn")
46
 
{
47
 
  int nargin = args.length ();
48
 
  octave_value retval;
49
 
 
50
 
  if (nargin != 1)
51
 
    print_usage ();
52
 
  else
53
 
    {
54
 
      const charMatrix cmat = args(0).char_matrix_value ();
55
 
 
56
 
      if (cmat.columns () > 16)
57
 
        error ("hex2num: expecting no more than a 16 character string");
58
 
      else if (! error_state)
59
 
        {
60
 
          octave_idx_type nr = cmat.rows ();
61
 
          octave_idx_type nc = cmat.columns ();
62
 
          ColumnVector m (nr);
63
 
 
64
 
          for (octave_idx_type i = 0; i < nr; i++)
65
 
            {
66
 
              uint64_t num = 0;
67
 
              for (octave_idx_type j = 0; j < nc; j++)
68
 
                {
69
 
                  unsigned char ch = cmat.elem (i, j);
70
 
 
71
 
                  if (isxdigit (ch))
72
 
                    {
73
 
                      num <<= 4;
74
 
                      if (ch >= 'a')
75
 
                        num += static_cast<uint64_t> (ch - 'a' + 10);
76
 
                      else if (ch >= 'A')
77
 
                        num += static_cast<uint64_t> (ch - 'A' + 10);
78
 
                      else
79
 
                        num += static_cast<uint64_t> (ch - '0');
80
 
                    }
81
 
                  else
82
 
                    {
83
 
                      error ("hex2num: illegal character found in string");
84
 
                      break;
85
 
                    }
86
 
                }
87
 
 
88
 
              if (error_state)
89
 
                break;
90
 
              else
91
 
                {
92
 
                  if (nc < 16)
93
 
                    num <<= (16 - nc) * 4;
94
 
 
95
 
                  m (i) = *reinterpret_cast<double *>(&num);
96
 
 
97
 
                }
98
 
            }
99
 
 
100
 
          if (! error_state)
101
 
            retval =  m;
102
 
        }
103
 
    }
104
 
 
105
 
  return retval;
106
 
}
107
 
 
108
 
/*
109
 
 
110
 
%!assert (hex2num(['c00';'bff';'000';'3ff';'400']),[-2:2]')
111
 
 
112
 
 */
113
 
 
114
 
 
115
 
// PKG_ADD: autoload ("num2hex", "hex2num.oct");
116
 
DEFUN_DLD (num2hex, args, ,
117
 
  "-*- texinfo -*-\n\
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\
121
 
\n\
122
 
@example\n\
123
 
num2hex ([-1, 1, e, Inf, NaN, NA]);\n\
124
 
@result{} \"bff0000000000000\n\
125
 
    3ff0000000000000\n\
126
 
    4005bf0a8b145769\n\
127
 
    7ff0000000000000\n\
128
 
    fff8000000000000\n\
129
 
    7ff00000000007a2\"\n\
130
 
@end example\n\
131
 
@seealso{hex2num, hex2dec, dec2hex}\n\
132
 
@end deftypefn")
133
 
{
134
 
  int nargin = args.length ();
135
 
  octave_value retval;
136
 
 
137
 
  if (nargin != 1)
138
 
    print_usage ();
139
 
  else
140
 
    {
141
 
      const ColumnVector v (args(0).vector_value ());
142
 
 
143
 
      if (! error_state)
144
 
        {
145
 
          octave_idx_type nr = v.length ();
146
 
          charMatrix m (nr, 16);
147
 
          const double *pv = v.fortran_vec ();
148
 
 
149
 
          for (octave_idx_type i = 0; i < nr; i++)
150
 
            {
151
 
              const uint64_t num = *reinterpret_cast<const uint64_t *> (pv++);
152
 
              for (octave_idx_type j = 0; j < 16; j++)
153
 
                {
154
 
                  unsigned char ch = 
155
 
                    static_cast<char> (num >> ((15 - j) * 4) & 0xF);
156
 
                  if (ch >= 10)
157
 
                    ch += 'a' - 10;
158
 
                  else
159
 
                    ch += '0';
160
 
 
161
 
                  m.elem (i, j) = ch;
162
 
                }
163
 
            }
164
 
          
165
 
          retval = octave_value (m, true);
166
 
        }
167
 
    }
168
 
 
169
 
  return retval;
170
 
}
171
 
 
172
 
/*
173
 
 
174
 
%!assert (num2hex (-2:2),['c000000000000000';'bff0000000000000';'0000000000000000';'3ff0000000000000';'4000000000000000'])
175
 
 
176
 
 */
177
 
 
178