comparison 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
comparison
equal deleted inserted replaced
31238:67cad4e8f866 31239:dd6b37f67db2
57 57
58 if (nargin == 0) 58 if (nargin == 0)
59 print_usage (); 59 print_usage ();
60 endif 60 endif
61 61
62 if (iscell (d)) 62 ## To avoid repeating a lot of code, including input validation, we call dec2bin.
63 d = cell2mat (d); 63 if (nargin == 2)
64 endif 64 d = dec2bin (d, len*4);
65 ## Create column vector for algorithm (output is always col. vector anyways) 65 else
66 d = d(:); 66 d = dec2bin (d);
67
68 lt_zero_idx = (d < 0);
69 if (any (lt_zero_idx))
70 ## FIXME: Need an algorithm that works with larger values such as int64.
71 if (any (d(lt_zero_idx) < -2^52))
72 error ("dec2hex: negative inputs cannot be less than -flintmax () / 2");
73 elseif (any (d(lt_zero_idx) < intmin ("int32")))
74 d(lt_zero_idx) += flintmax ();
75 elseif (any (d < intmin ("int16")))
76 d(lt_zero_idx) += double (intmax ("uint32")) + 1;
77 elseif (any (d < intmin ("int8")))
78 d(lt_zero_idx) += double (intmax ("uint16"))+ 1;
79 else
80 d(lt_zero_idx) += double (intmax ("uint8")) + 1;
81 endif
82 endif 67 endif
83 68
84 if (nargin == 1) 69 ## Left-pad with zeros to make the number of columns divisible by 4
85 hstr = dec2base (d, 16); 70 n = mod (columns (d), 4);
86 else 71 if (n > 0)
87 hstr = dec2base (d, 16, len); 72 d = [repmat("0", rows(d), 4-n), d];
88 endif 73 endif
74
75 d -= "0"; # convert to numeric
76 d = d(:, 1:4:end) * 8 + d(:, 2:4:end) * 4 + d(:, 3:4:end) * 2 + d(:, 4:4:end);
77 ## Elements of d are now in the range 0 to 15
78
79 hstr = "0123456789ABCDEF"(d+1); # convert to char and return
89 80
90 endfunction 81 endfunction
91 82
92 83
93 %!assert (dec2hex (2748), "ABC") 84 %!assert (dec2hex (2748), "ABC")
98 89
99 ## Test negative inputs 90 ## Test negative inputs
100 %!assert (dec2hex (-3), "FD") 91 %!assert (dec2hex (-3), "FD")
101 %!assert (dec2hex (-3, 1), "FD") 92 %!assert (dec2hex (-3, 1), "FD")
102 %!assert (dec2hex (-3, 3), "0FD") 93 %!assert (dec2hex (-3, 3), "0FD")
103 %!assert (dec2hex (-2^7 -1), "FF7F") 94 %!assert (dec2hex (-2^7 - 1), "FF7F")
104 %!assert (dec2hex (-2^15 -1), "FFFF7FFF") 95 %!assert (dec2hex (-2^15 - 1), "FFFF7FFF")
105 ## FIXME: Matlab returns longer string that begins with 'F' 96 %!assert (dec2hex (-2^31 - 1), "FFFFFFFF7FFFFFFF")
106 %!assert (dec2hex (-2^31 -1), "1FFFFF7FFFFFFF") 97 %!assert (dec2hex (-2^52), "FFF0000000000000")
107 ## FIXME: Matlab returns longer string that begins with 'FFF' 98 %!assert (dec2hex (-2^63), "8000000000000000")
108 %!assert (dec2hex (-2^52), "10000000000000") 99 %!assert (dec2hex (int64 (-2) ^ 63), "8000000000000000")
109 ## FIXME: Uncomment when support for int64 is added 100 %!assert (dec2hex (int64 (-2) ^ 63 - 1), "8000000000000000")
110 %!#assert (dec2hex (-2^63), 101 %!assert (dec2hex (int64 (-2) ^ 63 + 1), "8000000000000001")
111 %! "1000000000000000000000000000000000000000000000000000000000000000")
112 %!#test
113 %! assert (dec2hex (int64 (-2^63)),
114 %! "1000000000000000000000000000000000000000000000000000000000000000");
115 %!#test
116 %! assert (dec2hex (int64 (-2^63) -1),
117 %! "1000000000000000000000000000000000000000000000000000000000000000");
118 %!#test
119 %! assert (dec2hex (int64 (-2^63) +1),
120 %! "1000000000000000000000000000000000000000000000000000000000000001");
121 %!assert (dec2hex ([-1, -2; -3, -4]), ["FF"; "FD"; "FE"; "FC"]) 102 %!assert (dec2hex ([-1, -2; -3, -4]), ["FF"; "FD"; "FE"; "FC"])
122 %!assert (dec2hex ([1, 2; 3, -4]), ["01"; "03"; "02"; "FC"]) 103 %!assert (dec2hex ([1, 2; 3, -4]), ["01"; "03"; "02"; "FC"])
123 %!assert (dec2hex ({1, 2; 3, -4}), ["01"; "03"; "02"; "FC"]) 104 %!assert (dec2hex ({1, 2; 3, -4}), ["01"; "03"; "02"; "FC"])
124 105
125 ## Test input validation 106 ## Test input validation
126 %!error <Invalid call> dec2hex () 107 %!error <Invalid call> dec2hex ()
127 %!error <negative inputs cannot be less than> dec2hex (- flintmax ()) 108