Showing posts with label print32(). Show all posts
Showing posts with label print32(). Show all posts

Thursday, November 22, 2012

printHex(), print16(), print32() functions









    348 void printHex(const void* buf, size_t size)
    349 {
    350    const u_int8* ptr = reinterpret_cast(buf);
    351
    352    for (size_t i = 0; i < size; ++i)
    353    {
    354       printf("%02x%c", static_cast(ptr[i]),
    355              ((i + 1) & 15) == 0 || (i + 1) == size ? '\n' : ' ');
    356    }
    357 }
    358
    359 void print32(const void* buf, size_t size)
    360 {
    361    const size_t n = size / sizeof(u_int32);
    362    const u_int32* ptr = reinterpret_cast(buf);
    363
    364    for (size_t i = 0; i < n; ++i)
    365    {
    366       printf("%08x%c", static_cast(ptr[i]),
    367              ((i + 1) & 3) == 0 || (i + 1) == n ? '\n' : ' ');
    368    }
    369    if (n * sizeof(u_int32) != size)
    370    {
    371       printf("Extra data: ");
    372       printHex(ptr + n, size - n * sizeof(u_int32));
    373    }
    374 }
    375
    376 void print16(const void* buf, size_t size)
    377 {
    378    const size_t n = size / sizeof(u_int16);
    379    const u_int16* ptr = reinterpret_cast(buf);
    380
    381    for (size_t i = 0; i < n; ++i)
    382    {
    383       printf("%04x%c", static_cast(ptr[i]),
    384              ((i + 1) & 7) == 0 || (i + 1) == n ? '\n' : ' ');
    385    }
    386    if (n * sizeof(u_int16) != size)
    387    {
    388       printf("Extra data: ");
    389       printHex(ptr + n, size - n * sizeof(u_int16));
    390    }
    391 }

Labels