I already tried approach like this (result: tii-02):
String myString = (char*)cliMacAddr;
or this one (result: nothing):
void prntmac(){
Serial.print("MAC Address: ");
for (byte i = 0; i < 6; ++i) {
Serial.print(cliMacAddr[i], HEX);
if (i < 5)
Serial.print(':');
}
Serial.println();
}
I would like a function to return the MAC Address as String value.
String mac2String(byte ar[])
{
String s;
for (byte i = 0; i < 6; ++i)
{
char buf[3];
sprintf(buf, "%2X", cliMacAddr[i]);
s += buf;
if (i < 5) s += ':';
}
return s;
}
You do not need a String for that. The code to do that looks exactly like the code to display the MAC on the serial monitor, except that the destination for the data is different.
and use in other data entry/processing.
Put the tap dancing shoes away. EXACTLY what data entry/processing? The ONLY way that the MAC address is usable is as a collection of bytes. Converting that collection to a String and back to an array of bytes is a waste of resources.
{
String s;
for (byte i = 0; i < 6; ++i)
{
char buf[3];
sprintf(buf, "%2X", cliMacAddr[i]);
s += buf;
if (i < 5) s += ':';
}
return s;
}
not very efficient but it should work (I think)
Hey robtillaart it's working great, after 1 correction! Thanks!
String mac2String(byte ar[]){
String s;
for (byte i = 0; i < 6; ++i)
{
char buf[3];
sprintf(buf, "%2X", ar[i]);
s += buf;
if (i < 5) s += ':';
}
return s;
}
PaulS:
You do not need a String for that. The code to do that looks exactly like the code to display the MAC on the serial monitor, except that the destination for the data is different.
Put the tap dancing shoes away. EXACTLY what data entry/processing? The ONLY way that the MAC address is usable is as a collection of bytes. Converting that collection to a String and back to an array of bytes is a waste of resources.
It's because you can't get data in mentioned format
robtillaart:
Still PaulS has a point you do not need a String version of the byte array to display it on LCD or Serial,
send it over the internet or whatever.
Maybe, but I don't know else way to pass byte array to http query....
beic:
Maybe, but I don't know else way to pass byte array to http query....
you could use a char array which uses in general less memory than a String.
But if it works who cares, there is almost always another way to solve the same problem.
The reason PaulS and others (including me) are less font of Strings is that they can fragment the limited memory of the Arduino (esp UNO). We have seen too many sketches that "explode" due to the String class.
robtillaart:
you could use a char array which uses in general less memory than a String.
But if it works who cares, there is almost always another way to solve the same problem.
The reason PaulS and others (including me) are less font of Strings is that they can fragment the limited memory of the Arduino (esp UNO). We have seen too many sketches that "explode" due to the String class.
just a footnote,
What's your project about?
My project is about an array of DS18b20 temperature sensors (i2c), 2x Digital kWh meter trough IRQ, and all the info sent by ethernet to the Domoticz server.
Yeah, it is more struggling than fun, because I'm not so good in Java, Json and C programming, I'm in VB waters about 20 years now...
But you helped me a lot!
btw...I think that one is the only one String in my whole Sketch the rest are char definitions and I try to use dtostrf and sprintf anywhere if that is a good idea.