online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code   
Language
/****************************************************************************** Converting HEX bytes into flat string Credits to @Jonathan Leffler and @Jens *******************************************************************************/ #include <stdio.h> const unsigned char * MAC_HEX_TO_STR(char *MAC_ADDR); int main() { char *MAC_ADDR = "\x00\x22\xC7\xFF\xFF\x27"; /* Working solution #1 char str[13]; char *MAC_ADDR_NEW = str; %02x%02x%02x%02x%02x%02x // Small char %.2X%.2X%.2X%.2X%.2X%.2X // Capital char snprintf(str, sizeof(str), "%.2X%.2X%.2X%.2X%.2X%.2X", (unsigned char)MAC_ADDR[0], (unsigned char)MAC_ADDR[1], (unsigned char)MAC_ADDR[2], (unsigned char)MAC_ADDR[3], (unsigned char)MAC_ADDR[4], (unsigned char)MAC_ADDR[5]); printf(" ==>> %s\n ", MAC_ADDR_NEW); */ /* Working solution #2 char *MAC_ADDR = "\x00\x22\xC7\xFF\xFF\x27"; char *MAC_ADDR_NEW; asprintf (&MAC_ADDR_NEW, "%02x%02x%02x%02x%02x%02x", (uint8_t)MAC_ADDR[0], (uint8_t)MAC_ADDR[1], (uint8_t)MAC_ADDR[2], (uint8_t)MAC_ADDR[3], (uint8_t)MAC_ADDR[4], (uint8_t)MAC_ADDR[5]); printf("%s\n", MAC_ADDR_NEW); free(MAC_ADDR_NEW); // Good software hygiene is to free what you allocate. */ const unsigned char* test = MAC_HEX_TO_STR(MAC_ADDR); printf(" 2==>> %s\n ", test); return 0; } const unsigned char * MAC_HEX_TO_STR(char *MAC_ADDR){ // If static is not used, due to local variable is destroyed out of the function will return gibberish! static char str[13]; static char *MAC_ADDR_NEW = str; //%02x%02x%02x%02x%02x%02x // Small char //%.2X%.2X%.2X%.2X%.2X%.2X // Capital char snprintf(str, sizeof(str), "%.2X%.2X%.2X%.2X%.2X%.2X", (unsigned char)MAC_ADDR[0], (unsigned char)MAC_ADDR[1], (unsigned char)MAC_ADDR[2], (unsigned char)MAC_ADDR[3], (unsigned char)MAC_ADDR[4], (unsigned char)MAC_ADDR[5]); //printf(" RETURNED_MAC ==>> %s\n ", MAC_ADDR_NEW); //MAC_ADDR_NEW[13] = 0; // I don't know, I tried with or wirhout but no difference observed! return MAC_ADDR_NEW; }

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text
×

                

                

Program is not being debugged. Click "Debug" button to start program in debug mode.

#FunctionFile:Line
VariableValue
RegisterValue
ExpressionValue