Forum Discussion
Altera_Forum
Honored Contributor
15 years agoThe longer version (the one that got eaten by the 'internets') explained a little more.
There might be an easier way but this way does it (step by step) by extracting a pointer to the 32-bit value (&value), typecasting that pointer into a pointer to a 32-bit int (unsigned int *), then taking the value pointed to by this pointer (*). That value is passed to printf to print it as a hex value ("%08X"). The best part is that all the hard work is done by the pre-processor by just controlling the way that printf looks at the value. There is really no conversion done at all, it just prints it the way it sees it, only now it sees it as an int. All the 'easy' ways generally wind up with the value being actually converted to an int (e.g. 6.8 truncates into 6) then printing the value of the int. not what you want. This way winds up only being about 6 RISK machine instructions to load the value and format string into registers, then a call to '_printf'. I've always though the 'best' code is no code at all. Let the pre-processor do all the work!:)