21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

This function takes the following arguments:<br />

prefix<br />

String to be printed in front of the hexadecimal output. Subsequent lines of output<br />

are indented appropriately.<br />

str<br />

String to be printed, in binary. It is represented as an unsigned char * to make<br />

the code simpler. The caller will probably want to cast, or it can be easily rewritten<br />

to be a void *, which would require this code to cast this argument to a bytebased<br />

type for the array indexing to work correctly.<br />

len<br />

Number of bytes to print.<br />

This function prints out bytes as two characters, and it pairs bytes in groups of four.<br />

It will also print only 16 bytes per line. Modifying the appropriate preprocessor declarations<br />

at the top easily changes those parameters.<br />

Currently, this function writes to the standard output, but it can be modified to return<br />

a malloc( )’d string quite easily using sprintf( ) and putc( ) instead of printf( ) and<br />

putchar( ).<br />

In C++, you can print any data object in hexadecimal by setting the flag ios::hex<br />

using the setf( ) method on ostream objects (the unsetf( ) method can be used to<br />

clear flags). You might also want the values to print in all uppercase, in which case<br />

you should set the ios::uppercase flag. If you want a leading “0x” to print to denote<br />

hexadecimal, also set the flag ios::showbase. For example:<br />

cout.setf(ios::hex | ios::uppercase | ios::showbase);<br />

cout

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!