hex2num.cc

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2008-2012 David Bateman
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 3 of the License, or (at your
00010 option) any later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, see
00019 <http://www.gnu.org/licenses/>.
00020 
00021 */
00022 
00023 #ifdef HAVE_CONFIG_H
00024 #include <config.h>
00025 #endif
00026 
00027 #include <algorithm>
00028 
00029 #include "defun-dld.h"
00030 #include "error.h"
00031 #include "gripes.h"
00032 #include "oct-obj.h"
00033 #include "utils.h"
00034 
00035 DEFUN_DLD (hex2num, args, ,
00036   "-*- texinfo -*-\n\
00037 @deftypefn {Loadable Function} {@var{n} =} hex2num (@var{s})\n\
00038 Typecast the 16 character hexadecimal character string to an IEEE 754\n\
00039 double precision number.  If fewer than 16 characters are given the\n\
00040 strings are right padded with '0' characters.\n\
00041 \n\
00042 Given a string matrix, @code{hex2num} treats each row as a separate\n\
00043 number.\n\
00044 \n\
00045 @example\n\
00046 @group\n\
00047 hex2num ([\"4005bf0a8b145769\";\"4024000000000000\"])\n\
00048 @result{} [2.7183; 10.000]\n\
00049 @end group\n\
00050 @end example\n\
00051 @seealso{num2hex, hex2dec, dec2hex}\n\
00052 @end deftypefn")
00053 {
00054   int nargin = args.length ();
00055   octave_value retval;
00056 
00057   if (nargin != 1)
00058     print_usage ();
00059   else
00060     {
00061       const charMatrix cmat = args(0).char_matrix_value ();
00062 
00063       if (cmat.columns () > 16)
00064         error ("hex2num: S must be no more than 16 characters");
00065       else if (! error_state)
00066         {
00067           octave_idx_type nr = cmat.rows ();
00068           octave_idx_type nc = cmat.columns ();
00069           ColumnVector m (nr);
00070 
00071           for (octave_idx_type i = 0; i < nr; i++)
00072             {
00073               union
00074               {
00075                 uint64_t ival;
00076                 double dval;
00077               } num;
00078 
00079               num.ival = 0;
00080 
00081               for (octave_idx_type j = 0; j < nc; j++)
00082                 {
00083                   unsigned char ch = cmat.elem (i, j);
00084 
00085                   if (isxdigit (ch))
00086                     {
00087                       num.ival <<= 4;
00088                       if (ch >= 'a')
00089                         num.ival += static_cast<uint64_t> (ch - 'a' + 10);
00090                       else if (ch >= 'A')
00091                         num.ival += static_cast<uint64_t> (ch - 'A' + 10);
00092                       else
00093                         num.ival += static_cast<uint64_t> (ch - '0');
00094                     }
00095                   else
00096                     {
00097                       error ("hex2num: illegal character found in string S");
00098                       break;
00099                     }
00100                 }
00101 
00102               if (error_state)
00103                 break;
00104               else
00105                 {
00106                   if (nc < 16)
00107                     num.ival <<= (16 - nc) * 4;
00108 
00109                   m(i) = num.dval;
00110                 }
00111             }
00112 
00113           if (! error_state)
00114             retval =  m;
00115         }
00116     }
00117 
00118   return retval;
00119 }
00120 
00121 /*
00122 %!assert (hex2num(['c00';'bff';'000';'3ff';'400']),[-2:2]')
00123 */
00124 
00125 DEFUN_DLD (num2hex, args, ,
00126   "-*- texinfo -*-\n\
00127 @deftypefn {Loadable Function} {@var{s} =} num2hex (@var{n})\n\
00128 Typecast a double precision number or vector to a 16 character hexadecimal\n\
00129 string of the IEEE 754 representation of the number.  For example:\n\
00130 \n\
00131 @example\n\
00132 @group\n\
00133 num2hex ([-1, 1, e, Inf, NaN, NA]);\n\
00134 @result{} \"bff0000000000000\n\
00135     3ff0000000000000\n\
00136     4005bf0a8b145769\n\
00137     7ff0000000000000\n\
00138     fff8000000000000\n\
00139     7ff00000000007a2\"\n\
00140 @end group\n\
00141 @end example\n\
00142 @seealso{hex2num, hex2dec, dec2hex}\n\
00143 @end deftypefn")
00144 {
00145   int nargin = args.length ();
00146   octave_value retval;
00147 
00148   if (nargin != 1)
00149     print_usage ();
00150   else
00151     {
00152       const ColumnVector v (args(0).vector_value ());
00153 
00154       if (! error_state)
00155         {
00156           octave_idx_type nr = v.length ();
00157           charMatrix m (nr, 16);
00158           const double *pv = v.fortran_vec ();
00159 
00160           for (octave_idx_type i = 0; i < nr; i++)
00161             {
00162               union
00163               {
00164                 uint64_t ival;
00165                 double dval;
00166               } num;
00167 
00168               num.dval = *pv++;
00169 
00170               for (octave_idx_type j = 0; j < 16; j++)
00171                 {
00172                   unsigned char ch =
00173                     static_cast<char> (num.ival >> ((15 - j) * 4) & 0xF);
00174                   if (ch >= 10)
00175                     ch += 'a' - 10;
00176                   else
00177                     ch += '0';
00178 
00179                   m.elem (i, j) = ch;
00180                 }
00181             }
00182 
00183           retval = m;
00184         }
00185     }
00186 
00187   return retval;
00188 }
00189 
00190 /*
00191 %!assert (num2hex (-2:2),['c000000000000000';'bff0000000000000';'0000000000000000';'3ff0000000000000';'4000000000000000'])
00192 */
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines