diff scripts/strings/dec2hex.m @ 31239:dd6b37f67db2

Accept negative inputs to -2^63 for dec2bin and dec2hex (bug #63089) dec2bin.m: Accept negative inputs lower than -flintmax down to -2^63 Return 64-bit string in certain cases for Matlab compatibility Activate BISTs commented out earlier dec2hex.m: Remove repeated code and call dec2bin instead Accept negative inputs lower than -flintmax down to -2^63 Update and activate BISTs commented out earlier
author Arun Giridhar <arungiridhar@gmail.com>
date Sun, 25 Sep 2022 06:22:25 -0400
parents 5d3faba0342e
children 7018819318d1
line wrap: on
line diff
--- a/scripts/strings/dec2hex.m	Sat Sep 24 11:57:44 2022 +0200
+++ b/scripts/strings/dec2hex.m	Sun Sep 25 06:22:25 2022 -0400
@@ -59,34 +59,25 @@
     print_usage ();
   endif
 
-  if (iscell (d))
-    d = cell2mat (d);
-  endif
-  ## Create column vector for algorithm (output is always col. vector anyways)
-  d = d(:);
-
-  lt_zero_idx = (d < 0);
-  if (any (lt_zero_idx))
-    ## FIXME: Need an algorithm that works with larger values such as int64.
-    if (any (d(lt_zero_idx) < -2^52))
-      error ("dec2hex: negative inputs cannot be less than -flintmax () / 2");
-    elseif (any (d(lt_zero_idx) < intmin ("int32")))
-      d(lt_zero_idx) += flintmax ();
-    elseif (any (d < intmin ("int16")))
-      d(lt_zero_idx) += double (intmax ("uint32")) + 1;
-    elseif (any (d < intmin ("int8")))
-      d(lt_zero_idx) += double (intmax ("uint16"))+ 1;
-    else
-      d(lt_zero_idx) += double (intmax ("uint8")) + 1;
-    endif
+  ## To avoid repeating a lot of code, including input validation, we call dec2bin.
+  if (nargin == 2)
+    d = dec2bin (d, len*4);
+  else
+    d = dec2bin (d);
   endif
 
-  if (nargin == 1)
-    hstr = dec2base (d, 16);
-  else
-    hstr = dec2base (d, 16, len);
+  ## Left-pad with zeros to make the number of columns divisible by 4
+  n = mod (columns (d), 4);
+  if (n > 0)
+    d = [repmat("0", rows(d), 4-n), d];
   endif
 
+  d -= "0"; # convert to numeric
+  d = d(:, 1:4:end) * 8 + d(:, 2:4:end) * 4 + d(:, 3:4:end) * 2 + d(:, 4:4:end);
+  ## Elements of d are now in the range 0 to 15
+
+  hstr = "0123456789ABCDEF"(d+1); # convert to char and return
+
 endfunction
 
 
@@ -100,28 +91,18 @@
 %!assert (dec2hex (-3), "FD")
 %!assert (dec2hex (-3, 1), "FD")
 %!assert (dec2hex (-3, 3), "0FD")
-%!assert (dec2hex (-2^7 -1), "FF7F")
-%!assert (dec2hex (-2^15 -1), "FFFF7FFF")
-## FIXME: Matlab returns longer string that begins with 'F'
-%!assert (dec2hex (-2^31 -1), "1FFFFF7FFFFFFF")
-## FIXME: Matlab returns longer string that begins with 'FFF'
-%!assert (dec2hex (-2^52), "10000000000000")
-## FIXME: Uncomment when support for int64 is added
-%!#assert (dec2hex (-2^63),
-%!        "1000000000000000000000000000000000000000000000000000000000000000")
-%!#test
-%! assert (dec2hex (int64 (-2^63)),
-%!        "1000000000000000000000000000000000000000000000000000000000000000");
-%!#test
-%! assert (dec2hex (int64 (-2^63) -1),
-%!        "1000000000000000000000000000000000000000000000000000000000000000");
-%!#test
-%! assert (dec2hex (int64 (-2^63) +1),
-%!        "1000000000000000000000000000000000000000000000000000000000000001");
+%!assert (dec2hex (-2^7 - 1), "FF7F")
+%!assert (dec2hex (-2^15 - 1), "FFFF7FFF")
+%!assert (dec2hex (-2^31 - 1), "FFFFFFFF7FFFFFFF")
+%!assert (dec2hex (-2^52), "FFF0000000000000")
+%!assert (dec2hex (-2^63), "8000000000000000")
+%!assert (dec2hex (int64 (-2) ^ 63), "8000000000000000")
+%!assert (dec2hex (int64 (-2) ^ 63 - 1), "8000000000000000")
+%!assert (dec2hex (int64 (-2) ^ 63 + 1), "8000000000000001")
 %!assert (dec2hex ([-1, -2; -3, -4]), ["FF"; "FD"; "FE"; "FC"])
 %!assert (dec2hex ([1, 2; 3, -4]), ["01"; "03"; "02"; "FC"])
 %!assert (dec2hex ({1, 2; 3, -4}), ["01"; "03"; "02"; "FC"])
 
 ## Test input validation
 %!error <Invalid call> dec2hex ()
-%!error <negative inputs cannot be less than> dec2hex (- flintmax ())
+