Package ghidra.util
Class NumericUtilities
java.lang.Object
ghidra.util.NumericUtilities
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final BigIntegerstatic final longstatic final BigInteger -
Method Summary
Modifier and TypeMethodDescriptionstatic longstatic StringconvertBytesToString(byte[] bytes) Convert a byte array into a hexadecimal string.static StringconvertBytesToString(byte[] bytes, int start, int len, String delimeter) Convert a byte array into a hexadecimal string.static StringconvertBytesToString(byte[] bytes, String delimeter) Convert a byte array into a hexadecimal string.static StringconvertBytesToString(Iterable<Byte> bytes, String delimiter) Convert a bytes into a hexadecimal string.static StringconvertBytesToString(Iterator<Byte> bytes, String delimiter) Convert a bytes into a hexadecimal string.static StringconvertBytesToString(Stream<Byte> bytes, String delimiter) Convert a bytes into a hexadecimal string.static voidconvertHexStringToMaskedValue(AtomicLong msk, AtomicLong val, String hex, int n, int spaceevery, String spacer) static StringconvertMaskedValueToHexString(long msk, long val, int n, boolean truncate, int spaceevery, String spacer) Convert a masked value into a hexadecimal-ish string.static StringconvertMaskToHexString(long msk, int n, boolean truncate, int spaceevery, String spacer) Convert a mask to a hexadecimal-ish string.static byte[]convertStringToBytes(String hexString) Parse hexadecimal digits into a byte array.static StringformatNumber(long number, int radix) Rendernumberin different bases using the default signedness mode.static StringformatNumber(long number, int radix, SignednessFormatMode mode) Provide renderings ofnumberin different bases:0- rendersnumberas an escaped character sequence2- rendersnumberas abase-2integer8- rendersnumberas abase-8integer10- rendersnumberas abase-10integer16(default) - rendersnumberas abase-16integer Number Radix DEFAULT Mode Alias UNSIGNED Mode Value SIGNED Mode Value 100 2 UNSIGNED 1100100b 1100100b 100 8 UNSIGNED 144o 144o 100 10 SIGNED 100 100 100 16 UNSIGNED 64h 64h -1 2 UNSIGNED 1111111111111111111111111111111111111111111111111111111111111111b -1b -1 8 UNSIGNED 1777777777777777777777o -1o -1 10 SIGNED 18446744073709551615 -1 -1 16 UNSIGNED ffffffffffffffffh -1h -100 2 UNSIGNED 1111111111111111111111111111111111111111111111111111111110011100b -1100100b -100 8 UNSIGNED 1777777777777777777634o -144o -100 10 SIGNED 18446744073709551516 -100 -100 16 UNSIGNED ffffffffffffff9ch -64hstatic longgetUnsignedAlignedValue(long unsignedValue, long alignment) Get an unsigned aligned value corresponding to the specified unsigned value which will be greater than or equal the specified value.static booleanisFloatingPointType(Class<?> numClass) Determine if the provided Number class is a floating-point type.static booleanisFloatingPointType(Number number) Determine if the provided Number is a floating-point type -- Float or Double.static booleanisIntegerType(Class<?> numClass) Determine if the provided Number class is an integer type.static booleanisIntegerType(Number number) Determine if the provided Number is an integer type -- Byte, Short, Integer, or Long.static BigIntegerParses the given hex string as a BigIntge value, detecting whether or not it begins with a hex prefix, and if not, parses as a long int value.static longParses the given string as a hex long value, detecting whether or not it begins with a hex prefix, and if not, parses as a long int value.static longParses the given string as a numeric value, detecting whether or not it begins with a hex prefix, and if not, parses as a long int value.static longparseNumber(String numStr) Parses the given string as a numeric value, detecting whether or not it begins with a hex prefix, and if not, parses as a long int value.static LongparseNumber(String s, Long defaultValue) Parses the given string as a numeric value, detecting whether or not it begins with a hex prefix, and if not, parses as a long int value.static final StringtoHexString(long value) returns the value of the specified long as hexadecimal, prefixing with theHEX_PREFIX_xstring.static final StringtoHexString(long value, int size) returns the value of the specified long as hexadecimal, prefixing with theHEX_PREFIX_xstring.static final StringtoSignedHexString(long value) returns the value of the specified long as signed hexadecimal, prefixing with theHEX_PREFIX_xstring.static StringtoString(byte b) Convert the given byte into a two character String, padding with a leading 0 if needed.static BigIntegerunsignedLongToBigInteger(long value) Converts a unsigned long value, which is currently stored in a java signed long, into aBigInteger.static doubleunsignedLongToDouble(long val) Convert a long, treated as unsigned, to a double
-
Field Details
-
MAX_UNSIGNED_LONG
-
MAX_SIGNED_LONG
-
MAX_UNSIGNED_INT32_AS_LONG
public static final long MAX_UNSIGNED_INT32_AS_LONG- See Also:
-
-
Method Details
-
parseNumber
Parses the given string as a numeric value, detecting whether or not it begins with a hex prefix, and if not, parses as a long int value.- Parameters:
numStr- the number string- Returns:
- the long value or 0
-
parseNumber
Parses the given string as a numeric value, detecting whether or not it begins with a hex prefix, and if not, parses as a long int value.- Parameters:
s- the string to parsedefaultValue- the default value to use if the string cannot be parsed- Returns:
- the long value
-
parseLong
Parses the given string as a numeric value, detecting whether or not it begins with a hex prefix, and if not, parses as a long int value.- Parameters:
s- the string to parse- Returns:
- the long value
- Throws:
NumberFormatException- if the string is blank or has too many digits
-
parseHexLong
Parses the given string as a hex long value, detecting whether or not it begins with a hex prefix, and if not, parses as a long int value.- Parameters:
s- the string to parse- Returns:
- the long value
- Throws:
NumberFormatException- if the string is blank
-
parseHexBigInteger
Parses the given hex string as a BigIntge value, detecting whether or not it begins with a hex prefix, and if not, parses as a long int value.- Parameters:
s- the string to parse- Returns:
- the long value
- Throws:
NumberFormatException- if the string is blank
-
toHexString
returns the value of the specified long as hexadecimal, prefixing with theHEX_PREFIX_xstring.- Parameters:
value- the long value to convert- Returns:
- the string
-
toHexString
returns the value of the specified long as hexadecimal, prefixing with theHEX_PREFIX_xstring.- Parameters:
value- the long value to convertsize- number of bytes to be represented- Returns:
- the string
-
toSignedHexString
returns the value of the specified long as signed hexadecimal, prefixing with theHEX_PREFIX_xstring.- Parameters:
value- the long value to convert- Returns:
- the string
-
unsignedLongToBigInteger
Converts a unsigned long value, which is currently stored in a java signed long, into aBigInteger.In other words, the full 64 bits of the primitive java signed long is being used to store an unsigned value. This method converts this into a positive BigInteger value.
- Parameters:
value- java unsigned long value stuffed into a java signed long- Returns:
- new
BigIntegerwith the positive value of the unsigned long value
-
bigIntegerToUnsignedLong
-
unsignedLongToDouble
public static double unsignedLongToDouble(long val) Convert a long, treated as unsigned, to a double- Parameters:
val- the long to treat as unsigned and convert- Returns:
- the double
-
getUnsignedAlignedValue
public static long getUnsignedAlignedValue(long unsignedValue, long alignment) Get an unsigned aligned value corresponding to the specified unsigned value which will be greater than or equal the specified value.- Parameters:
unsignedValue- value to be alignedalignment- alignment- Returns:
- aligned value
-
convertMaskedValueToHexString
public static String convertMaskedValueToHexString(long msk, long val, int n, boolean truncate, int spaceevery, String spacer) Convert a masked value into a hexadecimal-ish string. Converts the data to hexadecimal, placing an X where a nibble is unknown. Where a nibble is partially defined, it is displayed as four bits in brackets []. Bits are displayed as x, or the defined value. For example, consider the mask 00001111:01011100, and the value 00001001:00011000. This will display asX8:[x0x1][10xx]. To see the correlation, consider the table:Display X8:[x0x1][10xx]Mask 00001111:01011100Value 00001000:00011000- Parameters:
msk- the maskval- the valuen- the number of nibbles, starting at the right. The example uses 4.truncate- true if leading Xs may be truncated. The example usesfalse.spaceevery- how many nibbles in spaced groups, 0 for no spaces. The example uses 2.spacer- the group separator, if applicable. The example uses':'.- Returns:
- the string representation
- See Also:
-
convertMaskToHexString
public static String convertMaskToHexString(long msk, int n, boolean truncate, int spaceevery, String spacer) Convert a mask to a hexadecimal-ish string. Converts the mask in a similar way toconvertMaskedValueToHexString(long, long, int, boolean, int, String). Philosophically, it is hexadecimal, but the only valid digits are 0 and F. Any partially-included nibble will be broken down into bracketed bits. Displaying masks in this way is convenient when shown proximal to related masked values.- Parameters:
msk- the maskn- the number of nibbles, starting at the righttruncate- true if leading Xs may be truncatedspaceevery- how many nibbles in spaced groups, 0 for no spacesspacer- the group separator, if applicable- Returns:
- the string representation
- See Also:
-
convertHexStringToMaskedValue
public static void convertHexStringToMaskedValue(AtomicLong msk, AtomicLong val, String hex, int n, int spaceevery, String spacer) - Parameters:
msk- an object to receive the resulting maskval- an object to receive the resulting valuehex- the input string to parsen- the number of nibbles to parse (they are stored right aligned in the result)spaceevery- how many nibbles are expected between spacersspacer- the spacer- See Also:
-
formatNumber
Rendernumberin different bases using the default signedness mode.This invokes formatNumber(long, int, SignednessFormatMode) with a
modeparameter ofSignednessFormatMode.DEFAULT.- Parameters:
number- The number to representradix- the base in whichnumberis represented- Returns:
- formatted string of the number parameter in provided radix base
- See Also:
-
formatNumber
Provide renderings ofnumberin different bases:0- rendersnumberas an escaped character sequence2- rendersnumberas abase-2integer8- rendersnumberas abase-8integer10- rendersnumberas abase-10integer16(default) - rendersnumberas abase-16integer
Number Radix DEFAULT Mode Alias UNSIGNED Mode Value SIGNED Mode Value 100 2 UNSIGNED 1100100b 1100100b 100 8 UNSIGNED 144o 144o 100 10 SIGNED 100 100 100 16 UNSIGNED 64h 64h -1 2 UNSIGNED 1111111111111111111111111111111111111111111111111111111111111111b -1b -1 8 UNSIGNED 1777777777777777777777o -1o -1 10 SIGNED 18446744073709551615 -1 -1 16 UNSIGNED ffffffffffffffffh -1h -100 2 UNSIGNED 1111111111111111111111111111111111111111111111111111111110011100b -1100100b -100 8 UNSIGNED 1777777777777777777634o -144o -100 10 SIGNED 18446744073709551516 -100 -100 16 UNSIGNED ffffffffffffff9ch -64h - Parameters:
number- The number to representradix- The base in whichnumberis representedmode- Specifies how the number is formatted with respect to its signed-ness- Returns:
- number string in the given base
-
convertStringToBytes
Parse hexadecimal digits into a byte array.- Parameters:
hexString- hexadecimal digits- Returns:
- numeric value as a byte array, or null if string contains invalid hex characters.
-
toString
Convert the given byte into a two character String, padding with a leading 0 if needed.- Parameters:
b- the byte- Returns:
- the byte string
-
convertBytesToString
Convert a byte array into a hexadecimal string.- Parameters:
bytes- byte array- Returns:
- hex string representation
-
convertBytesToString
Convert a byte array into a hexadecimal string.- Parameters:
bytes- byte arraydelimeter- the text between byte strings- Returns:
- hex string representation
-
convertBytesToString
Convert a byte array into a hexadecimal string.- Parameters:
bytes- byte arraystart- start indexlen- number of bytes to convertdelimeter- the text between byte strings- Returns:
- hex string representation
-
convertBytesToString
Convert a bytes into a hexadecimal string.- Parameters:
bytes- an iterator of bytesdelimiter- the text between byte strings; null is allowed- Returns:
- hex string representation
-
convertBytesToString
Convert a bytes into a hexadecimal string.- Parameters:
bytes- an iterable of bytesdelimiter- the text between byte strings; null is allowed- Returns:
- hex string representation
-
convertBytesToString
Convert a bytes into a hexadecimal string.- Parameters:
bytes- an stream of bytesdelimiter- the text between byte strings; null is allowed- Returns:
- hex string representation
-
isIntegerType
Determine if the provided Number is an integer type -- Byte, Short, Integer, or Long.- Parameters:
number- the object to check for for integer-type- Returns:
- true if the provided number is an integer-type, false otherwise
-
isIntegerType
Determine if the provided Number class is an integer type.- Parameters:
numClass- Class of an object- Returns:
- true if the class parameter is a integer type, false otherwise
-
isFloatingPointType
Determine if the provided Number is a floating-point type -- Float or Double.- Parameters:
number- the object to check for for floating-point-type- Returns:
- true if the provided number is a floating-point-type, false otherwise
-
isFloatingPointType
Determine if the provided Number class is a floating-point type.- Parameters:
numClass- Class of an object- Returns:
- true if the class parameter is a floating-point type, false otherwise
-