Vault
4.1
|
VHex is mainly a namespace for some global functions converting to/from hexadecimal strings and buffer display. More...
#include <vhex.h>
Public Member Functions | |
VHex (VTextIOStream *outputStream=NULL, int numBytesPerRow=16, int indentCount=2, bool labelsInHex=false, bool showASCIIValues=true) | |
Creates a hex dump object with specified parameters. | |
virtual | ~VHex () |
Destructor. | |
void | printHex (const Vu8 *buffer, Vs64 length, Vs64 offset=0) |
Prints a buffer of hex data according to the settings set at construction. | |
void | reset () |
Resets the object so it can be re-used for a brand new hex dump. | |
void | flush () |
Flushes any unwritten output. | |
Static Public Member Functions | |
static void | bufferToHexString (const Vu8 *buffer, Vs64 bufferLength, VString &s, bool wantLeading0x=false) |
Produces a hexadecimal string representation of the specified buffer data. | |
static void | hexStringToBuffer (const VString &hexDigits, Vu8 *buffer, bool hasLeading0x=false) |
Produces a buffer of bytes as specified by a supplied hexadecimal string representation. | |
static void | stringToHex (const VString &text, VString &hexDigits, bool wantLeading0x=false) |
Produces a hexadecimal string representation of the specified buffer data. | |
static void | hexToString (const VString &hexDigits, VString &text, bool hasLeading0x=false) |
Produces a buffer of bytes as specified by a supplied hexadecimal string representation. | |
static void | byteToHexString (Vu8 byteValue, VString &s) |
Produces a 2-character hexadecimal string representing the supplied byte value. | |
static void | byteToHexChars (Vu8 byteValue, char *highNibbleChar, char *lowNibbleChar) |
Produces two hexadecimal characters representing the supplied byte value. | |
static Vu8 | hexStringToByte (const char *twoHexDigits) |
Produces a byte value as specified by a supplied pair of hexadecimal chars. | |
static Vu8 | hexCharsToByte (char highNibbleChar, char lowNibbleChar) |
Produces a byte value as specified by a supplied pair of hexadecimal chars. | |
static char | nibbleToHexChar (Vu8 nibbleValue) |
Produces a char representing the low nibble of a supplied byte value. | |
static Vu8 | hexCharToNibble (char hexChar) |
Produces a byte value with a zero high nibble and a low nibble as specified by a supplied hex digit char. | |
static void | bufferToPrintableASCIIString (const Vu8 *buffer, Vs64 bufferLength, VString &s) |
Produces a string representation of the specified buffer data, where any byte in the printable ASCII range 0x20 to 0x7E is shown as its ASCII character, and anything else is a period ('. | |
static void | readHexDump (VTextIOStream &inputStream, VBinaryIOStream &outputStream) |
This function can be used to read a hex dump and create an in-memory buffer from it. |
VHex is mainly a namespace for some global functions converting to/from hexadecimal strings and buffer display.
You can also instantiate an object for hex dump generation.
The static functions can be used standalone; they build up functionality to convert between buffer and hex string, using simpler functions to convert between shorter sequences of bytes and characters.
If you want a pretty hex dump, instantiate a VHex object with formatting parameters, and then call its printHex() method with the desired output object.
VHex::VHex | ( | VTextIOStream * | outputStream = NULL , |
int | numBytesPerRow = 16 , |
||
int | indentCount = 2 , |
||
bool | labelsInHex = false , |
||
bool | showASCIIValues = true |
||
) |
Creates a hex dump object with specified parameters.
outputStream | the text stream to print to, or NULL for stdout |
numBytesPerRow | the number of data bytes to display per row of text output |
indentCount | the number of spaces to lead each row with |
labelsInHex | true if the offset labels should be given in hex, false for decimal |
showASCIIValues | true if each byte's ASCII equivalent should be displayed |
void VHex::bufferToHexString | ( | const Vu8 * | buffer, |
Vs64 | bufferLength, | ||
VString & | s, | ||
bool | wantLeading0x = false |
||
) | [static] |
Produces a hexadecimal string representation of the specified buffer data.
buffer | pointer to the data to parse |
bufferLength | the length of data to parse |
s | the string to format |
wantLeading0x | true if you want the string prefaced with the text "0x" |
Definition at line 17 of file vhex.cpp.
References VString::buffer(), byteToHexChars(), VString::postflight(), and VString::preflight().
void VHex::hexStringToBuffer | ( | const VString & | hexDigits, |
Vu8 * | buffer, | ||
bool | hasLeading0x = false |
||
) | [static] |
Produces a buffer of bytes as specified by a supplied hexadecimal string representation.
hexDigits | the hexadecimal string |
buffer | the buffer to fill (must be big enough!) |
hasLeading0x | true if the hexadecimal string starts with a leading "0x" |
Definition at line 47 of file vhex.cpp.
References VString::charAt(), hexCharsToByte(), and VString::length().
void VHex::stringToHex | ( | const VString & | text, |
VString & | hexDigits, | ||
bool | wantLeading0x = false |
||
) | [static] |
Produces a hexadecimal string representation of the specified buffer data.
text | the string to convert |
hexDigits | the returned hex string |
wantLeading0x | true if you want the string prefaced with the text "0x" |
Definition at line 74 of file vhex.cpp.
References bufferToHexString(), VString::getDataBufferConst(), and VString::length().
void VHex::hexToString | ( | const VString & | hexDigits, |
VString & | text, | ||
bool | hasLeading0x = false |
||
) | [static] |
Produces a buffer of bytes as specified by a supplied hexadecimal string representation.
hexDigits | the hexadecimal string |
text | the returned plain text string |
hasLeading0x | true if the hexadecimal string starts with a leading "0x" that must be ignored |
Definition at line 79 of file vhex.cpp.
References VString::getDataBuffer(), hexStringToBuffer(), VString::length(), VString::postflight(), and VString::preflight().
Produces a 2-character hexadecimal string representing the supplied byte value.
byteValue | the byte value to convert to hex |
s | the string to format |
Definition at line 92 of file vhex.cpp.
References byteToHexChars().
void VHex::byteToHexChars | ( | Vu8 | byteValue, |
char * | highNibbleChar, | ||
char * | lowNibbleChar | ||
) | [static] |
Produces two hexadecimal characters representing the supplied byte value.
byteValue | the byte value to convert to hex |
highNibbleChar | pointer to the char that will contain the high nibble hex digit |
lowNibbleChar | pointer to the char that will contain the low nibble hex digit |
Definition at line 102 of file vhex.cpp.
References nibbleToHexChar().
Vu8 VHex::hexStringToByte | ( | const char * | twoHexDigits | ) | [static] |
Produces a byte value as specified by a supplied pair of hexadecimal chars.
twoHexDigits | pointer to buffer containing (at least) two chars |
Definition at line 108 of file vhex.cpp.
References hexCharToNibble().
Vu8 VHex::hexCharsToByte | ( | char | highNibbleChar, |
char | lowNibbleChar | ||
) | [static] |
Produces a byte value as specified by a supplied pair of hexadecimal chars.
highNibbleChar | a hex digit char specifying the high nibble of the byte |
lowNibbleChar | a hex digit char specifying the low nibble of the byte |
Definition at line 116 of file vhex.cpp.
References hexCharToNibble().
char VHex::nibbleToHexChar | ( | Vu8 | nibbleValue | ) | [static] |
Vu8 VHex::hexCharToNibble | ( | char | hexChar | ) | [static] |
void VHex::bufferToPrintableASCIIString | ( | const Vu8 * | buffer, |
Vs64 | bufferLength, | ||
VString & | s | ||
) | [static] |
Produces a string representation of the specified buffer data, where any byte in the printable ASCII range 0x20 to 0x7E is shown as its ASCII character, and anything else is a period ('.
').
buffer | pointer to the data to parse |
bufferLength | the length of data to parse |
s | the string to format |
void VHex::readHexDump | ( | VTextIOStream & | inputStream, |
VBinaryIOStream & | outputStream | ||
) | [static] |
This function can be used to read a hex dump and create an in-memory buffer from it.
The input text is presumed to be in the format generated by VHex::printHex, and each time you call this function it will return a newly allocated buffer for the next chunk of the hex dump. A chunk is delineated by a blank line, so this function returns after reading a blank line, with the i/o mark ending at the next chunk header line. If the next line in the stream is blank when you call this function, the outputStream returned will be empty. If you call this repeatedly on an input file stream, you will eventually see a VEOFException thrown by the file reader at the end of the file. If this function encounters a line in an unexpected format, it will simply skip that line; this could allow you to annotate a hex dump. The lines this function expects are: 1. Lines of hex data in the form, detected by offset label, colon, hex data, ascii data e.g.: 00000000: 00 00 00 01 00 00 00 20 11 77 6F 72 6B 2E 69 6E .........work.in Only the hex bytes are processed; end of data is indicated by more than one space. 2. Blank lines (treated as end of chunk) 3. Anything else. (ignored)
Definition at line 157 of file vhex.cpp.
References hexCharsToByte(), VString::isEmpty(), VChar::isHexadecimal(), VString::isNotEmpty(), VString::length(), VTextIOStream::readLine(), VString::startsWith(), VString::trim(), and VBinaryIOStream::writeU8().
Prints a buffer of hex data according to the settings set at construction.
buffer | pointer to the buffer of data to dump |
length | the number of bytes of data to dump |
offset | byte offset in the buffer of the first byte of data to be dumped |
Definition at line 225 of file vhex.cpp.
References flush().