I want to replace the ":"(colon) in the MAC address with no char (remove the ":").
But I can't find any good solution.
String.replace(), can't replace with (void).
Note: String.replace(':',(char)0) //also doesn't work
String.remove() is ugly and requires that you know exactly where each iteration is (indexOf()), and this method doesn't return anything, it updates the string directly so you have to call : String.remove() for each occurence.
and String.substring() has most of the same problems as remove, except that it does return the substring, so you can do: String.substring(i,l).substring(i2,l)...
#include <WiFi.h>
String MAC;
char ssid[] = "WiFi SSID"
char psw[] = "WiFi PSW"
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
WiFi.begin(ssid,psw);
MAC = WiFi.macAddress();
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println("MAC Address unformatted")
Serial.println(MAC);
String MAC_f = MAC.replace(':', ' ');
// Now print the MAC without the ":"
Serial.println(MAC_f);
// This replaces the ':' with a space. I want to replace(remove) the ':' with no char
// Looking for an elegant, simple, 1 liner to accomplish this.
/* This looks horrible */
MAC_f = MAC.substring(0,2)+MAC.substring(3, 5)+MAC.substring(6, 8)+MAC.substring(9, 11)+MAC.substring(12, 14)+MAC.substring(15, 17);
/* .remove also looks terrible */
MAC_f = MAC;
MAC_f.remove(2,1);
MAC_f.remove(2,1);
// ...
}
-
Please do not attempt to answer questions in the comments. See How do comments work?. The comments on this question have been deleted, including the ones about "a special message". Appropriate action has been taken in regard to this message.Nick Gammon– Nick Gammon ♦2022年03月01日 06:59:21 +00:00Commented Mar 1, 2022 at 6:59
3 Answers 3
Don ́t use String()
manipulations inside loop()
. It causes heap fragmentation and program failure. Use a char array instead:
char MAC[18] = "01:23:45:67:89:AB"; // including terminating zero
for (int src = 0, dst = 0; src < sizeof(MAC); src++)
if (MAC[src] != ':') MAC[dst++] = MAC[src];
-
1It works because the terminating zero is copied to the end of the modified stringSBF– SBF2022年02月28日 13:09:59 +00:00Commented Feb 28, 2022 at 13:09
-
@Instigator in which way is this inefficient? Do you know what
String.replace()
does?Sim Son– Sim Son2022年02月28日 13:13:03 +00:00Commented Feb 28, 2022 at 13:13
As stated by Mat, the empty string in C++ is ""
. This works:
MAC_f = MAC;
MAC_f.replace(":", "");
That being said, I concur with SBF's answer: String
object are not
friendly with the Arduino's memory. SBF's solution is better than
String::replace()
.
-
@Instigator you know the difference between
'x'
and"x"
, right.String.replace()
takes String arguments, not char as you wrote. arduino.cc/reference/en/language/variables/data-types/string/…Sim Son– Sim Son2022年02月28日 13:21:10 +00:00Commented Feb 28, 2022 at 13:21
others a right that String is not totally Arduino friendly, WiFi.macAddress() returns String, SO
though written for ESP32 this is an approach i used
// Get the station interface MAC address.
// @return String MAC, no camas
String getMacAddress(void)
{
WiFi.mode(WIFI_AP_STA); // required to read NVR before WiFi.begin()
uint8_t baseMac[6];
esp_read_mac( baseMac, ESP_MAC_WIFI_STA ); // Get MAC address for WiFi station
char macStr[18] = { 0 };
sprintf(macStr, "%02X%02X%02X%02X%02X%02X", baseMac[0], baseMac[1], baseMac[2], baseMac[3], baseMac[4], baseMac[5]);
return String(macStr);
}
// END getMacAddress()
-
this maybe is how it should be done, but it doesn't answer the question2022年03月02日 05:46:54 +00:00Commented Mar 2, 2022 at 5:46