[LLVMdev] Fixed with hex numbers with raw_ostream

Jakob Stoklund Olesen stoklund at 2pi.dk
Fri May 18 13:44:23 PDT 2012


On May 18, 2012, at 11:50 AM, Nick Kledzik <kledzik at apple.com> wrote:

> I've been using the nifty:
>   DEBUG(errs() << "Here is the state of things:\n");
> style of optional logging, but ran into an issue where I want to dump a table of information.  The problem is getting the columns to line up, since the raw_ostream methods write numbers as variable length.  
> 
> I've worked up a patch that adds two new methods to raw_ostream:
> 
> /// write_hex - Output \arg N as ten char hexadecimal string, including
> /// 0x prefix (e.g. 0x12345678 or 0x00000001).
> raw_ostream &write_hex32(uint32_t N);
> 
> /// write_hex - Output \arg N as 18 char hexadecimal string, including
> /// 0x prefix (e.g. 0x0123456789abcdef or 0x0000000000000001).
> raw_ostream &write_hex64(uint64_t N);
> 
> 
> Is there already some way to do this level of formatting with raw_ostream?

You can get the full awesomeness of printf with include/llvm/Support/Format.h:

OS << format("%016" PRIx64, N);

I don't know if there is a significant performance difference.

/jakob




More information about the llvm-dev mailing list