Sprintf is doing something weird with my ints

Hi,
I'm looking for some clarification on the best way to take the 6 byte mac address of my wifi module and convert it to a char array so that I can create a unique url with it. After much time banging my head against the wall, I've managed to convert it to integers but going from integers to characters has finally brought me to a point where I require a sanity check.

The section of code that's giving me trouble is here:

byte mac[6];
int macIdentifier[6];
char macIdentifierChar[32];

WiFi.macAddress(mac);
getMacIdentifier();

void getMacIdentifier(){
  for (int i = 5; i>=0; i--){
    if(mac[i] < 16){
      Serial.print("getMacIdentifier() has failed");
    }
  }
  for(int i = 5;  i>=0; i--){
    macIdentifier[i] = (int)mac[i];
    Serial.println(macIdentifier[i]);  
    }
    sprintf(macIdentifierChar, "%i", "%i", "%i", "%i", "%i", "%i", macIdentifier[5], macIdentifier[4], macIdentifier[3], macIdentifier[2], macIdentifier[1], macIdentifier[0]);
    Serial.println(macIdentifierChar);

}

In the serial monitor it prints this:

02:28:52.553 -> 196
02:28:52.553 -> 221
02:28:52.553 -> 87
02:28:52.553 -> 184
02:28:52.553 -> 177
02:28:52.553 -> 40
02:28:52.553 -> 111248

Any thoughts or advice would be greatly appreciated.

sprintf(macIdentifierChar, "%i", "%i", "%i", "%i", "%i", "%i", macIdentifier[5], macIdentifier[4], macIdentifier[3], macIdentifier[2], macIdentifier[1], macIdentifier[0]);
  

Shouldn't that be

sprintf(macIdentifierChar, "%i,%i,%i,%i,%i,%i", macIdentifier[5], macIdentifier[4], macIdentifier[3], macIdentifier[2], macIdentifier[1], macIdentifier[0]);

1 Like

YES! THANK YOU UKHELIBOB! Not all heroes wear capes. Long live the 2am code sprint!

I gather that it worked :grinning:

Expect someone to be along soon to suggest that you should have used snprintf() instead of sprintf()

That and macIdentifier can be eliminated.

But a big :+1: for making macIdentifierChar an actual buffer with enough space.

Is that correct?

:rofl:
Yes!, I would suggest also to use HEX as output format instead decimal because usual MAC address is in the format 1A:3B:56:78:9A:BC

Here is a simple snprintf example compared to sprintf


void setup() {
  Serial.begin(115200); //for debug
  for (int i = 10; i > 0; i--) {
    delay(500);
    Serial.print(i); Serial.print(' ');
  }
  Serial.println("started");

  char b[5];
  sprintf(b, "%s", "BigTest");
  Serial.println(b);
  Serial.println(F("sprintf Buffer overflow..."));

  snprintf(b, 5, "%s", "BigTest");
  Serial.println(b);
  Serial.println(F("snprintf truncated output, no Buffer overflow"));
  Serial.println();

  char c[8];
  unsigned long l = 123456789;
  Serial.println(F("Test for snprintf truncation. snprintf of 123456789"));
  if (snprintf(c, sizeof(c), "%lu", l) >= sizeof(c)) {
    Serial.println(F("Warning result string truncated."));
  }
  Serial.println(c);
}

void loop() {
}

Turn up the warning level. If you had compiler warnings set to 'All' you would have been warned about this (potential) mistake:

sketch_jul01a.ino: In function 'void getMacIdentifier()':
sketch_jul01a.ino:23:172: warning: format '%i' expects argument of type 'int', but argument 3 has type 'const char*' [-Wformat=]
   sprintf(macIdentifierChar, "%i", "%i", "%i", "%i", "%i", "%i", macIdentifier[5], macIdentifier[4], macIdentifier[3], macIdentifier[2], macIdentifier[1], macIdentifier[0]);
                                                                                                                                                                            ^

sketch_jul01a.ino:23:172: warning: too many arguments for format [-Wformat-extra-args]