public class

Formatter

extends Object
java.lang.Object
   ↳ com.pnfsoftware.jeb.util.format.Formatter

Class Overview

A collection of binary and number formatting and escaping methods.

Summary

Public Constructors
Formatter()
Public Methods
static String byteArrayToHexString(byte[] data, int pos)
static String byteArrayToHexString(byte[] data)
static String byteArrayToHexString(byte[] data, int pos, int end)
static String escapeByte(int v)
static String escapeBytes(byte[] bytes, int offset, int size)
static String escapeBytes(byte[] bytes)
static String escapeCharacter(char c, boolean keepUnicodePrintables)
Escape a character, if necessary.
static String escapeCharacter(char c)
Escape a string, keeping unicode printable characters.
static String escapeString(CharSequence s)
Escape a string.
static String escapeString(String s, boolean keepUnicodePrintables)
Escape a string.
static String escapeString(String s, int directionalityEnclosure, boolean keepUnicodePrintables, Set<Character> doNotEscapeList)
Escape a string.
static String escapeToJavaStringArray(Collection<? extends Object> objects)
static String formatBinaryBlock(byte[] data, int offset, int size, long offsetDelta, boolean is64bit)
Format a block of bytes as a buffer of hexadecimal lines.
static String formatBinaryBlock(byte[] data)
Format a block of bytes.
static String formatBinaryBlock(byte[] data, int offset, int size, int offsetDelta)
Format a block of bytes.
static String formatBinaryBlock(byte[] data, int offset, int size)
Format a block of bytes.
static String formatBinaryLine(byte[] data)
Format a block of bytes.
static String formatBinaryLine(byte[] data, int offset, int size)
Format a block of bytes.
static String formatBinaryLine(byte[] data, int offset, int size, int minVirtualSize)
Format a block of bytes as a hexadecimal string representation.
static String formatBinaryLineTruncate(byte[] data, int offset, int size, int fixedCount)
static String formatBinaryLineTruncate(byte[] data, int offset, int size, int maxSize, char truncateChar)
static String formatHexNumbers(Collection<? extends Number> list)
static String formatNumbers(Collection<? extends Number> list, int base, String pfx, String sfx)
static byte[] hexStringToByteArray(String s)
Convert a series of hexadecimal String to a byte array.
static byte[] hexStringToByteArray(String s, int pos, int end)
Convert a series of hexadecimal String to a byte array.
static String integerToAlphaString(int n)
Convert an integer to an alphabetic string, using the following pattern:
 a: 0
 b: 1
 ...
static boolean isPrintableChar(char c)
Determine if a character is printable.
static String toHexString(long v, boolean upperCase)
Convert a long to its hex representation.
static String toHexString(int v, boolean upperCase)
Convert a int to its hex representation.
static String toHexString(int v, boolean upperCase, int padZero)
Convert a int to its hex representation.
static String toHexString(long v, boolean upperCase, int padZero)
Convert a long to its hex representation.
static String unescapeString(String s)
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors

public Formatter ()

Public Methods

public static String byteArrayToHexString (byte[] data, int pos)

public static String byteArrayToHexString (byte[] data)

public static String byteArrayToHexString (byte[] data, int pos, int end)

public static String escapeByte (int v)

public static String escapeBytes (byte[] bytes, int offset, int size)

public static String escapeBytes (byte[] bytes)

public static String escapeCharacter (char c, boolean keepUnicodePrintables)

Escape a character, if necessary.

Parameters
c the character to be escaped
keepUnicodePrintables if true, a printable unicode, non-ascii character will be left unescaped
Returns
  • a string representing the escaped character

public static String escapeCharacter (char c)

Escape a string, keeping unicode printable characters.

public static String escapeString (CharSequence s)

Escape a string. Unicode printable characters are not escaped (refer to isPrintableChar(char)). Strings containing RTL characters are enclosed in 0+202A (LEFT-TO-RIGHT EMBEDDING), and 0+202C (POP DIRECTIONAL FORMATTING).

Refer to escapeString(String, int, boolean, Set).

Parameters
s a string
Returns
  • the escaped string

public static String escapeString (String s, boolean keepUnicodePrintables)

Escape a string. Strings containing RTL characters are enclosed in 0+202A (LEFT-TO-RIGHT EMBEDDING), and 0+202C (POP DIRECTIONAL FORMATTING).

Refer to escapeString(String, int, boolean, Set).

Parameters
s a string
keepUnicodePrintables if true, unicode printable characters are not escaped (refer to isPrintableChar(char)).
Returns
  • the escaped string

public static String escapeString (String s, int directionalityEnclosure, boolean keepUnicodePrintables, Set<Character> doNotEscapeList)

Escape a string.

Parameters
s the string to be escaped
directionalityEnclosure if non-0, a string containing RTL (right-to-left) characters will be enclosed in special Unicode characters to force directionality when embedding the string into another string:
- 0: nothing added, directionality when embedding will be determined from the string and its context
- 1: force left-to-right for embedding
- 2: force right-to-left for embedding
keepUnicodePrintables if true, unicode printable characters are not escaped (refer to isPrintableChar(char)), eg, KCJ/Russian/etc. character sets
doNotEscapeList optional collection of characters that won't be escaped
Returns
  • the escaped string

public static String escapeToJavaStringArray (Collection<? extends Object> objects)

public static String formatBinaryBlock (byte[] data, int offset, int size, long offsetDelta, boolean is64bit)

Format a block of bytes as a buffer of hexadecimal lines. Each line represents the 16 bytes of buffer data; it is prefixed by an optional offset (address) base and suffixed by the Ascii representation of the bytes. Note that non-printable, non-Ascii characters are represented as dots.

Parameters
data bytes buffer
offset beginning of bytes sequence to be formatted
size number of bytes to be formatted
offsetDelta an offset delta
is64bit indicates if the prefix offset should be formatted as a 64-bit or 32-bit value
Returns
  • a formatted string

public static String formatBinaryBlock (byte[] data)

Format a block of bytes. Same as formatBinaryBlock(data, 0, data.length)

Parameters
data bytes buffer
Returns
  • a formatted string

public static String formatBinaryBlock (byte[] data, int offset, int size, int offsetDelta)

public static String formatBinaryBlock (byte[] data, int offset, int size)

Format a block of bytes. Same as formatBinaryBlock(data, offset, size, 0)

Parameters
data bytes buffer
offset beginning of bytes sequence to be formatted
size number of bytes to be formatted
Returns
  • a formatted string

public static String formatBinaryLine (byte[] data)

Format a block of bytes. Same as formatBinaryLine(data, 0, data.length, 0).

Parameters
data bytes buffer
Returns
  • a formatted string

public static String formatBinaryLine (byte[] data, int offset, int size)

Format a block of bytes. Same as formatBinaryLine(data, 0, size, size).

public static String formatBinaryLine (byte[] data, int offset, int size, int minVirtualSize)

Format a block of bytes as a hexadecimal string representation. Bytes will be represented space-separated.

Parameters
data bytes buffer
offset beginning of bytes sequence to be formatted
size number of bytes to be formatted
minVirtualSize minimum number of bytes that the resulting string is supposed to represent; phantom bytes (formatted as empty space) will be appended if necessary
Returns
  • a formatted string

public static String formatBinaryLineTruncate (byte[] data, int offset, int size, int fixedCount)

public static String formatBinaryLineTruncate (byte[] data, int offset, int size, int maxSize, char truncateChar)

public static String formatHexNumbers (Collection<? extends Number> list)

public static String formatNumbers (Collection<? extends Number> list, int base, String pfx, String sfx)

public static byte[] hexStringToByteArray (String s)

Convert a series of hexadecimal String to a byte array. Remove potential "h" suffix and "0x" prefix.

Parameters
s Input String
Returns
  • the byte array

public static byte[] hexStringToByteArray (String s, int pos, int end)

Convert a series of hexadecimal String to a byte array. Input must be padded with zeroes. If data is invalid, a null array is returned.

 hexStringToByteArray("1234ABCD", 0, 8) => {0x12, 0x23, 0xAB, 0xCD}
 hexStringToByteArray("1234ABCD", 0, 4) => {0x12, 0x23}
 hexStringToByteArray("1234ABCD", 0, 5) => null
 

Parameters
s Input String
pos the start parsing position
end the end parsing position
Returns
  • the byte array

public static String integerToAlphaString (int n)

Convert an integer to an alphabetic string, using the following pattern:

 a: 0
 b: 1
 ...
 z: 25
 aa: 26
 ...
 az: 51
 ba: 52
 ...
 zzz: 26-1+26*26
 aaa: 26+26*26
 ...
 aaaa: 26+26*26+26*26*26
 ...
 
Negative numbers will generate a string prefixed by '-'.

public static boolean isPrintableChar (char c)

Determine if a character is printable. This method defines a character as `printable` IFF:
- it is not an ISO control character
- it is not a surrogate pair (low or high)
- it is not considered a special Unicode character

Parameters
c the input character
Returns
  • true if the character is printable

public static String toHexString (long v, boolean upperCase)

Convert a long to its hex representation. Equivalent of "%X", but much faster.

Parameters
v long to convert
upperCase true if the hex must be in upper case
Returns
  • The hex representation

public static String toHexString (int v, boolean upperCase)

Convert a int to its hex representation. Equivalent of "%08X", but much faster.

Parameters
v int to convert
upperCase true if the hex must be in upper case
Returns
  • The hex representation

public static String toHexString (int v, boolean upperCase, int padZero)

Convert a int to its hex representation. Equivalent of "%08X", but much faster.

Parameters
v int to convert
upperCase true if the hex must be in upper case
padZero indicate the number of characters that will be output (will pad with zeroes)
Returns
  • The hex representation

public static String toHexString (long v, boolean upperCase, int padZero)

Convert a long to its hex representation. Equivalent of "%08X", but much faster.

Parameters
v long to convert
upperCase true if the hex must be in upper case
padZero indicate the number of characters that will be output (will pad with zeroes)
Returns
  • The hex representation

public static String unescapeString (String s)

Throws
ParseException