Package ghidra.util
Class NumericUtilities
- java.lang.Object
- 
- ghidra.util.NumericUtilities
 
- 
 public final class NumericUtilities extends java.lang.Object
- 
- 
Field SummaryFields Modifier and Type Field Description static java.math.BigIntegerMAX_SIGNED_LONGstatic java.math.BigIntegerMAX_UNSIGNED_LONG
 - 
Method SummaryAll Methods Static Methods Concrete Methods Modifier and Type Method Description static longbigIntegerToUnsignedLong(java.math.BigInteger value)static java.lang.StringconvertBytesToString(byte[] bytes)Convert a byte array into a hexadecimal string.static java.lang.StringconvertBytesToString(byte[] bytes, int start, int len, java.lang.String delimeter)Convert a byte array into a hexadecimal string.static java.lang.StringconvertBytesToString(byte[] bytes, java.lang.String delimeter)Convert a byte array into a hexadecimal string.static java.lang.StringconvertBytesToString(java.lang.Iterable<java.lang.Byte> bytes, java.lang.String delimiter)Convert a bytes into a hexadecimal string.static java.lang.StringconvertBytesToString(java.util.Iterator<java.lang.Byte> bytes, java.lang.String delimiter)Convert a bytes into a hexadecimal string.static java.lang.StringconvertBytesToString(java.util.stream.Stream<java.lang.Byte> bytes, java.lang.String delimiter)Convert a bytes into a hexadecimal string.static voidconvertHexStringToMaskedValue(java.util.concurrent.atomic.AtomicLong msk, java.util.concurrent.atomic.AtomicLong val, java.lang.String hex, int n, int spaceevery, java.lang.String spacer)static java.lang.StringconvertMaskedValueToHexString(long msk, long val, int n, boolean truncate, int spaceevery, java.lang.String spacer)Convert a masked value into a hexadecimal-ish string.static java.lang.StringconvertMaskToHexString(long msk, int n, boolean truncate, int spaceevery, java.lang.String spacer)Convert a mask to a hexadecimal-ish string.static byte[]convertStringToBytes(java.lang.String hexString)Parse hexadecimal digits into a byte array.static java.lang.StringformatNumber(long number, int radix)Rendernumberin different bases using the default signedness mode.static java.lang.StringformatNumber(long number, int radix, SignednessFormatMode mode)Provide renderings ofnumberin different bases: 0 - rendersnumberas an escaped character sequence 2 - rendersnumberas a base-2 integer 8 - rendersnumberas a base-8 integer 10 - rendersnumberas a base-10 integer 16 (default) - rendersnumberbase-16 integer NumberRadixDEFAULT Mode AliasUNSIGNED Mode ValueSIGNED Mode Value 1002UNSIGNED1100100b1100100b 1008UNSIGNED144o144o 10010SIGNED100100 10016UNSIGNED64h64h -12UNSIGNED1111111111111111111111111111111111111111111111111111111111111111b-1b -18UNSIGNED1777777777777777777777o-1o -110SIGNED18446744073709551615-1 -116UNSIGNEDffffffffffffffffh-1h -1002UNSIGNED1111111111111111111111111111111111111111111111111111111110011100b-1100100b -1008UNSIGNED1777777777777777777634o-144o -10010SIGNED18446744073709551516-100 -10016UNSIGNEDffffffffffffff9ch-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(java.lang.Class<?> numClass)Determine if the provided Number class is a floating-point type.static booleanisFloatingPointType(java.lang.Number number)Determine if the provided Number is a floating-point type -- Float or Double.static booleanisIntegerType(java.lang.Class<?> numClass)Determine if the provided Number class is an integer type.static booleanisIntegerType(java.lang.Number number)Determine if the provided Number is an integer type -- Byte, Short, Integer, or Long.static java.math.BigIntegerparseHexBigInteger(java.lang.String numStr)static longparseHexLong(java.lang.String numStr)static longparseLong(java.lang.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(java.lang.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 longparseOctLong(java.lang.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 java.lang.StringtoHexString(long value)returns the value of the specified long as hexadecimal, prefixing with the HEX_PREFIX_x string.static java.lang.StringtoHexString(long value, int size)returns the value of the specified long as hexadecimal, prefixing with the HEX_PREFIX_x string.static java.lang.StringtoSignedHexString(long value)returns the value of the specified long as signed hexadecimal, prefixing with the HEX_PREFIX_x string.static java.lang.StringtoString(byte b)Convert the given byte into a two character String, padding with a leading 0 if needed.static java.math.BigIntegerunsignedLongToBigInteger(long value)
 
- 
- 
- 
Method Detail- 
parseNumberpublic static long parseNumber(java.lang.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.
 - 
parseLongpublic static long parseLong(java.lang.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.
 - 
parseOctLongpublic static long parseOctLong(java.lang.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.
 - 
parseHexLongpublic static long parseHexLong(java.lang.String numStr) 
 - 
parseHexBigIntegerpublic static java.math.BigInteger parseHexBigInteger(java.lang.String numStr) 
 - 
toHexStringpublic static final java.lang.String toHexString(long value) returns the value of the specified long as hexadecimal, prefixing with the HEX_PREFIX_x string.- Parameters:
- value- the long value to convert
 
 - 
toHexStringpublic static final java.lang.String toHexString(long value, int size)returns the value of the specified long as hexadecimal, prefixing with the HEX_PREFIX_x string.- Parameters:
- value- the long value to convert
- size- number of bytes to be represented
 
 - 
toSignedHexStringpublic static final java.lang.String toSignedHexString(long value) returns the value of the specified long as signed hexadecimal, prefixing with the HEX_PREFIX_x string.- Parameters:
- value- the long value to convert
 
 - 
unsignedLongToBigIntegerpublic static final java.math.BigInteger unsignedLongToBigInteger(long value) 
 - 
bigIntegerToUnsignedLongpublic static final long bigIntegerToUnsignedLong(java.math.BigInteger value) 
 - 
getUnsignedAlignedValuepublic 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:
- value- value to be aligned
- alignment- alignment
- Returns:
- aligned value
 
 - 
convertMaskedValueToHexStringpublic static java.lang.String convertMaskedValueToHexString(long msk, long val, int n, boolean truncate, int spaceevery, java.lang.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 mask
- val- the value
- n- the number of nibbles, starting at the right. The example uses 4.
- truncate- true if leading Xs may be truncated. The example uses- false.
- 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(long, int, boolean, int, String),- convertHexStringToMaskedValue(AtomicLong, AtomicLong, String, int, int, String)
 
 - 
convertMaskToHexStringpublic static java.lang.String convertMaskToHexString(long msk, int n, boolean truncate, int spaceevery, java.lang.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 mask
- n- the number of nibbles, starting at the right
- truncate- true if leading Xs may be truncated
- spaceevery- how many nibbles in spaced groups, 0 for no spaces
- spacer- the group separator, if applicable
- Returns:
- the string representation
- See Also:
- convertMaskedValueToHexString(long, long, int, boolean, int, String),- convertHexStringToMaskedValue(AtomicLong, AtomicLong, String, int, int, String)
 
 - 
convertHexStringToMaskedValuepublic static void convertHexStringToMaskedValue(java.util.concurrent.atomic.AtomicLong msk, java.util.concurrent.atomic.AtomicLong val, java.lang.String hex, int n, int spaceevery, java.lang.String spacer)- Parameters:
- msk- an object to receive the resulting mask
- val- an object to receive the resulting value
- hex- the input string to parse
- n- the number of nibbles to parse (they are stored right aligned in the result)
- spaceevery- how many nibbles are expected between spacers
- spacer- the spacer
- See Also:
- convertMaskedValueToHexString(long, long, int, boolean, int, String),- convertMaskToHexString(long, int, boolean, int, String)
 
 - 
formatNumberpublic static java.lang.String formatNumber(long number, int radix)Rendernumberin different bases using the default signedness mode.This invokes formatNumber(long, int, SignednessFormatMode) with a mode parameter of SignednessFormatMode.DEFAULT. - Parameters:
- number- The number to represent
- radix- the base in which- numberis represented
- Returns:
- formatted string of the number parameter in provided radix base
- See Also:
- formatNumber(long, int, SignednessFormatMode)
 
 - 
formatNumberpublic static java.lang.String formatNumber(long number, int radix, SignednessFormatMode mode)Provide renderings ofnumberin different bases:-  0 - renders numberas an escaped character sequence
-  2 - renders numberas a base-2 integer
-  8 - renders numberas a base-8 integer
-  10 - renders numberas a base-10 integer
-  16 (default) - renders numberbase-16 integer
 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 represent
- radix- The base in which- numberis represented
- mode- Specifies how the number is formatted with respect to its signed-ness
- Returns:
- number string in the given base
 
-  0 - renders 
 - 
convertStringToBytespublic static byte[] convertStringToBytes(java.lang.String hexString) 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.
 
 - 
toStringpublic static java.lang.String toString(byte b) Convert the given byte into a two character String, padding with a leading 0 if needed.- Parameters:
- b- the byte
- Returns:
- the byte string
 
 - 
convertBytesToStringpublic static java.lang.String convertBytesToString(byte[] bytes) Convert a byte array into a hexadecimal string.- Parameters:
- bytes- byte array
- Returns:
- hex string representation
 
 - 
convertBytesToStringpublic static java.lang.String convertBytesToString(byte[] bytes, java.lang.String delimeter)Convert a byte array into a hexadecimal string.- Parameters:
- bytes- byte array
- delimeter- the text between byte strings
- Returns:
- hex string representation
 
 - 
convertBytesToStringpublic static java.lang.String convertBytesToString(byte[] bytes, int start, int len, java.lang.String delimeter)Convert a byte array into a hexadecimal string.- Parameters:
- bytes- byte array
- start- start index
- len- number of bytes to convert
- delimeter- the text between byte strings
- Returns:
- hex string representation
 
 - 
convertBytesToStringpublic static java.lang.String convertBytesToString(java.util.Iterator<java.lang.Byte> bytes, java.lang.String delimiter)Convert a bytes into a hexadecimal string.- Parameters:
- bytes- an iterator of bytes
- delimiter- the text between byte strings; null is allowed
- Returns:
- hex string representation
 
 - 
convertBytesToStringpublic static java.lang.String convertBytesToString(java.lang.Iterable<java.lang.Byte> bytes, java.lang.String delimiter)Convert a bytes into a hexadecimal string.- Parameters:
- bytes- an iterable of bytes
- delimiter- the text between byte strings; null is allowed
- Returns:
- hex string representation
 
 - 
convertBytesToStringpublic static java.lang.String convertBytesToString(java.util.stream.Stream<java.lang.Byte> bytes, java.lang.String delimiter)Convert a bytes into a hexadecimal string.- Parameters:
- bytes- an stream of bytes
- delimiter- the text between byte strings; null is allowed
- Returns:
- hex string representation
 
 - 
isIntegerTypepublic static boolean isIntegerType(java.lang.Number number) 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
 
 - 
isIntegerTypepublic static boolean isIntegerType(java.lang.Class<?> numClass) 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
 
 - 
isFloatingPointTypepublic static boolean isFloatingPointType(java.lang.Number number) 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
 
 - 
isFloatingPointTypepublic static boolean isFloatingPointType(java.lang.Class<?> numClass) 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
 
 
- 
 
-