35 DEFUN (hex2num, args, ,
37 @deftypefn {Built-in Function} {@var{n} =} hex2num (@var{s})\n\
38 @deftypefnx {Built-in Function} {@var{n} =} hex2num (@var{s}, @var{class})\n\
39 Typecast the 16 character hexadecimal character string to an IEEE 754\n\
40 double precision number. If fewer than 16 characters are given the\n\
41 strings are right padded with @qcode{'0'} characters.\n\
43 Given a string matrix, @code{hex2num} treats each row as a separate\n\
48 hex2num ([\"4005bf0a8b145769\"; \"4024000000000000\"])\n\
49 @result{} [2.7183; 10.000]\n\
53 The optional argument @var{class} can be passed as the string\n\
54 @qcode{\"single\"} to specify that the given string should be interpreted as\n\
55 a single precision number. In this case, @var{s} should be an 8 character\n\
56 hexadecimal string. For example: \n\
60 hex2num ([\"402df854\"; \"41200000\"], \"single\")\n\
61 @result{} [2.7183; 10.000]\n\
64 @seealso{num2hex, hex2dec, dec2hex}\n\
67 int nargin = args.length ();
70 if (nargin < 1 || nargin > 2)
72 else if (nargin == 2 && ! args(1).is_string ())
73 error (
"hex2num: CLASS must be a string");
76 const charMatrix cmat = args(0).char_matrix_value ();
77 std::string prec = (nargin == 2) ? args(1).string_value () :
"double";
78 bool is_single = (prec ==
"single");
82 error (
"hex2num: S must be no more than %d characters", nchars);
83 else if (prec !=
"double" && prec !=
"single")
84 error (
"hex2num: CLASS must be either \"double\" or \"single\"");
106 unsigned char ch = cmat.
elem (i, j);
112 num.ival +=
static_cast<uint32_t
> (ch -
'a' + 10);
114 num.ival +=
static_cast<uint32_t
> (ch -
'A' + 10);
116 num.ival +=
static_cast<uint32_t
> (ch -
'0');
120 error (
"hex2num: illegal character found in string S");
130 num.ival <<= (nchars - nc) * 4;
155 unsigned char ch = cmat.
elem (i, j);
161 num.ival +=
static_cast<uint64_t
> (ch -
'a' + 10);
163 num.ival +=
static_cast<uint64_t
> (ch -
'A' + 10);
165 num.ival +=
static_cast<uint64_t
> (ch -
'0');
169 error (
"hex2num: illegal character found in string S");
179 num.ival <<= (nchars - nc) * 4;
199 DEFUN (num2hex, args, ,
201 @deftypefn {Built-in Function} {@var{s} =} num2hex (@var{n})\n\
202 Typecast a double or single precision number or vector to a 8 or 16\n\
203 character hexadecimal string of the IEEE 754 representation of the number.\n\
208 num2hex ([-1, 1, e, Inf])\n\
209 @result{} \"bff0000000000000\n\
212 7ff0000000000000\"\n\
216 If the argument @var{n} is a single precision number or vector, the returned\n\
217 string has a length of 8. For example:\n\
221 num2hex (single ([-1, 1, e, Inf]))\n\
222 @result{} \"bf800000\n\
228 @seealso{hex2num, hex2dec, dec2hex}\n\
231 int nargin = args.length ();
236 else if (args(0).is_single_type ())
260 static_cast<char>(num.ival >> ((nchars - 1 - j) * 4) & 0xF);
297 static_cast<char>(num.ival >> ((nchars - 1 - j) * 4) & 0xF);