Skip to content

Instantly share code, notes, and snippets.

@gsauthof
Last active October 3, 2016 20:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gsauthof/a5794849d6faadf80ed4a6b2d920e3e3 to your computer and use it in GitHub Desktop.
Save gsauthof/a5794849d6faadf80ed4a6b2d920e3e3 to your computer and use it in GitHub Desktop.
comparison of boost hex algorithm and printf conversion specifier
// re: http://stackoverflow.com/questions/28489153/how-to-portably-compute-a-sha1-hash-in-c/39833022#39833022
#include <boost/uuid/sha1.hpp>
#include <boost/detail/endian.hpp>
#include <boost/algorithm/hex.hpp>
#include <boost/range/iterator_range_core.hpp>
#include <boost/endian/conversion.hpp>
#include <iostream>
#include <stdio.h>
using namespace std;
int main(int argc, char **argv)
{
unsigned i = 1;
unsigned j = boost::endian::native_to_big(i);
boost::algorithm::hex(boost::make_iterator_range(
reinterpret_cast<const char*>(&j),
reinterpret_cast<const char*>(&j+1)),
ostream_iterator<char>(cout)); cout << '\n';
printf("%08x\n", i);
printf("%08x\n", j);
return 0;
}
/* Output:
00000001
00000001
01000000
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment