Wifi mac address As String to Char*

What am I doing wrong and how do I correct it ?

String s = WiFi.macAddress();

snprintf (msg, 75, "Mac Address is %s",s);

Serial.println(msg);

The above gives me an error.

But this works.

char* s = "xx:xx:xx:xx:xx";

snprintf (msg, 75, ""Mac Address is %s"",s);

Serial.println(msg);

What should I do to use it with snprintf?

s.c_str()

Thanks for the reply.

Already tried but didn't work

did something like this,

try 1)

snprintf (msg, 75, "Mac Address is " +WiFi.macAddress.c_str());

try 2)

char* s = WiFi.macAddress.c_str();

snprintf (msg, 75, "Mac Address is %s " s );

neither worked. Did I miss anything?

snprintf (msg, 75, "Mac Address is %s ", s );

arguments of snprintf are target, target size, format string and then format parameters. so not + but ,

Just do this:

Serial.print("Mac Address is ");
Serial.println(WiFi.macAddress());

It's silly to go to so much trouble just to try to set up your message for a single Serial.println() call.

pert:
Just do this:

Serial.print("Mac Address is ");

Serial.println(WiFi.macAddress());




It's silly to go to so much trouble just to try to set up your message for a single Serial.println() call.

I did that , But I don't use msg buffer for printing in my project. I did Serial.print just to simplify and test.

My project specifically needs use of snprintf (msg, 75, "Mac Address is %s " s ).

OK, I understand now.

Juraj:
snprintf (msg, 75, "Mac Address is %s ", s );

arguments of snprintf are target, target size, format string and then format parameters. so not + but ,

Can you please help me with an example.

try 2, is ok. only the , is missing

I am curious, what output can’t take two prints? You could use CStringBuilder from my StreamLib

Finally could get it to work.
Here is the code.

#include <ESP8266WiFi.h>

void setup() {

char* mychar;
char msg[200];

Serial.begin(230400);
String s = WiFi.macAddress(); // Use Mac ID as a mychar
int slen = ss.length();
mychar = &s[0];

snprintf (msg, 75, “Mac Address is %s”, mychar);
Serial.println();
Serial.println(msg);

}

void loop() {
// put your main code here, to run repeatedly:
delay(100);

}

1 Like

mychar = &s[0];
is same as
mychar = s.c_str();

and I am still curious, what output you think can't take two prints?

Sure , If someone here could really explain. I am not sure too.

I_think:
Sure , If someone here could really explain. I am not sure too.

we don't know how do you want to use the resulting string