GP-3265 fix bit twiddling in NumericUtilities.toHexString()

This commit is contained in:
dev747368
2023-03-27 21:02:18 +00:00
parent 6f35c7ec47
commit 7bb10114df
2 changed files with 12 additions and 3 deletions

View File

@@ -15,12 +15,13 @@
*/
package ghidra.util;
import java.math.BigInteger;
import java.util.*;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.math.BigInteger;
import org.apache.commons.collections4.IteratorUtils;
import org.apache.commons.lang3.StringUtils;
@@ -217,7 +218,7 @@ public final class NumericUtilities {
*/
public final static String toHexString(long value, int size) {
if (size > 0 && size < 8) {
value &= -1L >> (8 * (8 - size));
value &= -1L >>> (8 * (8 - size));
}
return HEX_PREFIX_x + Long.toHexString(value);
}

View File

@@ -15,7 +15,8 @@
*/
package ghidra.util;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.util.*;
import java.util.stream.Collectors;
@@ -314,6 +315,13 @@ public class NumericUtilitiesTest {
}
@Test
public void testToHexString() {
for (int sizeofValue = 1; sizeofValue <= 8; sizeofValue++) {
assertEquals("0x" + "ff".repeat(sizeofValue), NumericUtilities.toHexString(-1, sizeofValue));
}
}
private void asssertBytesEquals(byte[] expected, byte[] actual) {
String errorMessage = "Byte arrays not equal - exptected: " + Arrays.toString(expected) +