1

I have a function that returns a unsigned char chMAC[6]; which is the mac address and i print it out as

printf("Mac: %x",chMAC[0]);
printf("%x",chMAC[1]);
printf("%x",chMAC[2]);
printf("%x",chMAC[3]);
printf("%x",chMAC[4]);
printf("%x\n",chMAC[5]);

And i get an output as Mac: B827E82D398E which is the actual mac address, but now i need to get that value as a string to pass to a sql parameter and i don't know how, since i need to add : in between. such as Mac: B8:27:E8:2D:39:8E

i bet this is easy, but i am still learning C.

1
  • 1
    Look at sprintf(), or better yet, snprintf(). Commented Aug 31, 2015 at 21:51

3 Answers 3

3

You probably want all the bytes to be displayed as two characters:

%2x

but with a leading 0 instead of space:

%02x

You can string this all together in one printf call

printf("Mac: %02X:%02X:%02X:%02X:%02X:%02X\n"
       , chMAC[0], chMAC[1], chMAC[2], chMAC[3], chMAC[4], chMAC[5]);

If you want the text to go to a sting buffer instead of stdout do this:

char buffer[32];

snprintf(buffer, sizeof(buffer)
        , "Mac: %02X:%02X:%02X:%02X:%02X:%02X\n"
        , chMAC[0], chMAC[1], chMAC[2], chMAC[3], chMAC[4], chMAC[5]);
Sign up to request clarification or add additional context in comments.

Comments

3

You have all the pieces there, you just need to string them into the right order. Instead of using 6 separate printf() statements, pull it into one statement with all the formatting:

printf("Mac: %02X:%02X:%02X:%02X:%02X:%02X\n", 
             chMAC[0], chMAC[1], chMAC[2], chMAC[3], chMAC[4], chMAC[5]);

The "02" in the "%02X" formatting statements will put a leading zero if the value is <15; the capital X will make the alphabetic Hex digits into capitals (which is the usual convention when passing MAC addresses).

To send the resulting string to a buffer instead of to stdout, call sprintf (or even better, snprintf) with the same formatting string.

char mac_str[24];
snprintf(mac_str, sizeof(mac_str), "Mac: %02X:%02X:%02X:%02X:%02X:%02X\n", 
              chMAC[0], chMAC[1], chMAC[2], chMAC[3], chMAC[4], chMAC[5]);

1 Comment

He asked how to store it in a string, not how to print it.
0

why all the separate calls?

newlength = sprintf(mac, '%x:%x:%x:%x:%x:%x\n', chMAC[1], etc....)

You can have multiple %whatever format characters in a single printf/sprintf call...

3 Comments

i read the documentation on sprintf but it retunres the number of displayed chars, If successful, the total number of characters written is returned excluding the null-character appended at the end of the string, otherwise a negative number is returned in case of failure.
You're missing the output buffer argument.
yeah, sorry. been buried in php-land too long. fixing up the sample.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.