Show debugHexStr.c syntax highlighted
#include "debugHexStr.h"
#include <stdlib.h>
static void charToHex(char x, void *string) {
char *one = (char*) string;
char *two = one + 1;
static char kToHexTable[] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
*two = kToHexTable[ x & 0x0F ];
*one = kToHexTable[ ( x >> 4 ) & 0x0F ];
}
const char* debugHexStr(const void *input, size_t inputSize) {
static char internalBuffer[4*1024];
static char *buffer = internalBuffer;
static size_t bufferSize = sizeof(internalBuffer);
if (!input || !inputSize) return "";
size_t requiredSize = (inputSize * 2) + 1;
// Enlarge the internal buffer if necessary.
if (requiredSize > bufferSize) {
if (buffer != internalBuffer)
free(buffer);
buffer = malloc(requiredSize);
bufferSize = requiredSize;
}
const char *inputIt = (const char*)input;
const char *inputEnd = inputIt + inputSize;
char *output = buffer;
for( ; inputIt != inputEnd; ++inputIt ) {
charToHex( *inputIt, output );
output += 2;
}
*output = 0;
return buffer;
}
See more files for this project here