int main() { const int n = 123456789 ; print_with_commas(n) ; std::cout << '\n' ; { // method two; works for all numeric types // see: http://en.cppreference.com/w/cpp/locale/numpunct/grouping struct with_commas : std::numpunct<char> { protected: // group every three digits before the decimal point virtual std::string do_grouping() const override { return "\3"; } // use ',' as the group seperator virtual char do_thousands_sep() const override { return ',' ; } }; // see: http://en.cppreference.com/w/cpp/io/basic_ios/imbue // http://en.cppreference.com/w/cpp/locale/locale/locale overload (7) // the locale calls delete on the facet when it is destroyed. std::cout.imbue( std::locale( std::cout.getloc(), new with_commas ) ); std::cout << n << '\n' ; } }
/******************************************************************************* * * PRINT_WITH_COMMAS * * This is a quick hack to print positive decimal numbers with thousands * separators. It calls itself recursively to print from left-to-right * whenever the value is >1000 and thus needs a comma. */ void print_with_commas( uint num ) { if( num < 1000 ) printf( "%d", num ); else { print_with_commas( num/1000 ); printf( ",%03d", num%1000 ); } }
int startTest(const char* vf) { uchar *vd; // a pointer to our loaded vector data int ret = 0; // our return -- non-zero for any failure int datalength; // length of the vector data file printf( "\n" ); // give us a blank line beneath the command invocation gcm_initialize(); // initialize our GCM library once before first use // attempt to load the test vectors file and report any problems switch ( datalength = load_file_into_ram( vf, &vd ) ) { case -1: printf( "Test vector file \"%s\" not found!\n", vf ); return datalength; // return status to OS case -2: printf( "Unable to allocate RAM for vector file!\n" ); return datalength; // return status to OS case -3: printf( "Error reading test vector file into memory!\n" ); return datalength; // return status to OS default: print_with_commas( datalength ); printf( " bytes of test vector data read.\n\n" ); } // run through and verify all of the NIST AES-GCM test vectors ret = verify_gcm( vd ); free( vd ); // release our test vector file allocation // print the total number of tests that passed. print_with_commas( testcount ); printf( " tests performed.\n\n" ); // and deliver the final test suite outcome. printf( "NIST AES-GCM validation test suite: " ); if( ret ) printf( "FAILED!!\n" ); else printf( "PASSED!!\n" ); return( ret ); // exit the program returning status to caller }
// method one; works for integer types void print_with_commas( unsigned int n ) { if( n < 1000 ) { std::cout << n ; } else { int remainder = n % 1000 ; print_with_commas( n / 1000 ) ; std::cout << ',' << remainder ; } }