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.\n\
42 If fewer than 16 characters are given the strings are right padded with\n\
43 @qcode{'0'} characters.\n\
45 Given a string matrix, @code{hex2num} treats each row as a separate number.\n\
49 hex2num ([\"4005bf0a8b145769\"; \"4024000000000000\"])\n\
50 @result{} [2.7183; 10.000]\n\
54 The optional argument @var{class} can be passed as the string\n\
55 @qcode{\"single\"} to specify that the given string should be interpreted as\n\
56 a single precision number. In this case, @var{s} should be an 8 character\n\
57 hexadecimal string. For example:\n\
61 hex2num ([\"402df854\"; \"41200000\"], \"single\")\n\
62 @result{} [2.7183; 10.000]\n\
65 @seealso{num2hex, hex2dec, dec2hex}\n\
68 int nargin = args.length ();
71 if (nargin < 1 || nargin > 2)
73 else if (nargin == 2 && ! args(1).is_string ())
74 error (
"hex2num: CLASS must be a string");
77 const charMatrix cmat = args(0).char_matrix_value ();
78 std::string prec = (nargin == 2) ? args(1).string_value () :
"double";
79 bool is_single = (prec ==
"single");
83 error (
"hex2num: S must be no more than %d characters", nchars);
84 else if (prec !=
"double" && prec !=
"single")
85 error (
"hex2num: CLASS must be either \"double\" or \"single\"");
107 unsigned char ch = cmat.
elem (i, j);
113 num.ival +=
static_cast<uint32_t
> (ch -
'a' + 10);
115 num.ival +=
static_cast<uint32_t
> (ch -
'A' + 10);
117 num.ival +=
static_cast<uint32_t
> (ch -
'0');
121 error (
"hex2num: illegal character found in string S");
131 num.ival <<= (nchars - nc) * 4;
156 unsigned char ch = cmat.
elem (i, j);
162 num.ival +=
static_cast<uint64_t
> (ch -
'a' + 10);
164 num.ival +=
static_cast<uint64_t
> (ch -
'A' + 10);
166 num.ival +=
static_cast<uint64_t
> (ch -
'0');
170 error (
"hex2num: illegal character found in string S");
180 num.ival <<= (nchars - nc) * 4;
200 DEFUN (num2hex, args, ,
202 @deftypefn {Built-in Function} {@var{s} =} num2hex (@var{n})\n\
203 Typecast a double or single precision number or vector to a 8 or 16\n\
204 character hexadecimal string of the IEEE 754 representation of the number.\n\
210 num2hex ([-1, 1, e, Inf])\n\
211 @result{} \"bff0000000000000\n\
214 7ff0000000000000\"\n\
218 If the argument @var{n} is a single precision number or vector, the returned\n\
219 string has a length of 8. For example:\n\
223 num2hex (single ([-1, 1, e, Inf]))\n\
224 @result{} \"bf800000\n\
230 @seealso{hex2num, hex2dec, dec2hex}\n\
233 int nargin = args.length ();
238 else if (args(0).is_single_type ())
262 static_cast<char>(num.ival >> ((nchars - 1 - j) * 4) & 0xF);
299 static_cast<char>(num.ival >> ((nchars - 1 - j) * 4) & 0xF);
OCTINTERP_API void print_usage(void)
#define DEFUN(name, args_name, nargout_name, doc)
void error(const char *fmt,...)
T & elem(octave_idx_type n)
octave_idx_type rows(void) const
octave_idx_type length(void) const
Number of elements in the array.
const T * fortran_vec(void) const
octave_idx_type columns(void) const