Mercurial > octave
comparison scripts/strings/dec2hex.m @ 31242:96ad887ae4f8 stable
dec2bin.m and dec2hex.m: graft bugfixes from dd6b37f67db2 to stable (bug #63089)
Fix behavior of dec2bin and dec2hex for negative integer-type inputs
author | Arun Giridhar <arungiridhar@gmail.com> |
---|---|
date | Wed, 28 Sep 2022 17:00:43 -0400 |
parents | 796f54d4ddbf |
children | 7018819318d1 |
comparison
equal
deleted
inserted
replaced
31232:adfbd487d0f6 | 31242:96ad887ae4f8 |
---|---|
22 ## <https://www.gnu.org/licenses/>. | 22 ## <https://www.gnu.org/licenses/>. |
23 ## | 23 ## |
24 ######################################################################## | 24 ######################################################################## |
25 | 25 |
26 ## -*- texinfo -*- | 26 ## -*- texinfo -*- |
27 ## @deftypefn {} {} dec2hex (@var{d}) | 27 ## @deftypefn {} {@var{hstr} =} dec2hex (@var{d}) |
28 ## @deftypefnx {} {} dec2hex (@var{d}, @var{len}) | 28 ## @deftypefnx {} {@var{hstr} =} dec2hex (@var{d}, @var{len}) |
29 ## Return a string representing the conversion of the integer @var{d} to a | 29 ## Return a string representing the conversion of the integer @var{d} to a |
30 ## hexadecimal (base16) number. | 30 ## hexadecimal (base16) number. |
31 ## | 31 ## |
32 ## If @var{d} is negative, return the hexadecimal equivalent of the two's | 32 ## If @var{d} is negative, return the hexadecimal equivalent of the two's |
33 ## complement binary value of @var{d}. | 33 ## complement binary value of @var{d}. |
51 ## @end example | 51 ## @end example |
52 ## | 52 ## |
53 ## @seealso{hex2dec, dec2base, dec2bin} | 53 ## @seealso{hex2dec, dec2base, dec2bin} |
54 ## @end deftypefn | 54 ## @end deftypefn |
55 | 55 |
56 function h = dec2hex (d, len) | 56 function hstr = dec2hex (d, len) |
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 h = dec2base (d, 16); | 70 n = mod (columns (d), 4); |
86 else | 71 if (n > 0) |
87 h = 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 |