Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-10 07:49:55

0001 #include <sstream>
0002 #include <iostream>
0003 #include <iomanip>
0004 #include <cstdint>
0005 #include <cassert>
0006 
0007 // seems htonl dont need this header
0008 //#include <arpa/inet.h>
0009 
0010 /**
0011 hexlify
0012 -------
0013 
0014 Dump the bytes of an object 
0015 
0016 **/
0017 
0018 std::string hexlify(const void* obj, size_t size, bool reverse)
0019 { 
0020     const unsigned char * const bytes = static_cast<const unsigned char *>(obj);
0021     std::stringstream ss ; 
0022     for(size_t i=0 ; i < size ; i++) ss << std::setw(2) << std::hex << std::setfill('0') << unsigned(bytes[reverse ? size - 1 - i : i]) ; 
0023     return ss.str(); 
0024 }
0025 
0026 /**
0027 test_network_order_ntohl_htonl
0028 --------------------------------
0029 
0030 **/
0031 
0032 void test_network_order_ntohl_htonl()
0033 {
0034     uint32_t hostlong = 0x12abcdef ; 
0035     uint32_t netlong = htonl(hostlong) ; 
0036 
0037     std::cout 
0038        << " std::hex "
0039        << " hostlong " << std::hex << hostlong 
0040        << " netlong " << std::hex << netlong 
0041        << std::endl 
0042        ;
0043 
0044     std::cout 
0045        << " hexlify "
0046        << " hostlong " << hexlify(&hostlong,4,true)
0047        << " netlong "  << hexlify(&netlong, 4,true) 
0048        << std::endl 
0049        ;
0050 
0051     uint32_t hostlong2 = ntohl(netlong) ;
0052     uint32_t netlong2 = htonl(hostlong2) ; 
0053 
0054     assert( hostlong == hostlong2 ); 
0055     assert( netlong == netlong2 ); 
0056 }
0057 
0058 int main(int argc, char** argv)
0059 {
0060     test_network_order_ntohl_htonl();
0061     return 0 ; 
0062 }
0063 
0064 // gcc test_network_order_ntohl_htonl.cc -lstdc++ -o /tmp/test_network_order_ntohl_htonl && /tmp/test_network_order_ntohl_htonl