It's just a few days since I started using Arduino IDE ( programming an ESP32 ). As I learn, I am trying to write some snippets. I wanted to store the Chip ID of ESP32 into a variable (preferably string?) so that I can generate an unique device name. I looked through the ESP32 example programs and came across this program which just prints the Chip ID to the serial console.

  uint64_t chipid;

  chipid = ESP.getEfuseMac(); //The chip ID is essentially its MAC address(length: 6 bytes).
  Serial.printf("ESP32 Chip ID = %04X", (uint16_t)(chipid >> 32)); //print High 2 bytes
  Serial.printf("%08X\n", (uint32_t)chipid); //print Low 4bytes.

Now this works fine but I needed to store it into a variable in the following format "ESP32-0A1B3C4D5E6F" ( the last part of which is the Chip/MAC ID which is 6 bytes ). I went about some reading and currently reading and managed to get this far

  char ssid1[15];
  char ssid2[15];

  uint64_t chipid = ESP.getEfuseMac(); // The chip ID is essentially its MAC address(length: 6 bytes).
  uint16_t chip = (uint16_t)(chipid >> 32);

  snprintf(ssid1, 15, "%04X", chip);
  snprintf(ssid2, 15, "%08X", (uint32_t)chipid);


I am trying to read this which seems to be an excellent article on handling Arduino strings but just being a few days old with Arduino/c++ I am having some difficulty in wrapping my head around some of the concepts. I come from a Python, PHP, Javascript background so I guess this is gona take a little bit of a time. I am getting the output that I want in the serial console but now I would need this in this format (MCUDEVICE-0A1B3C4D5E6F). I am sure this code can be written in a much nicer, cleaner manner but for now I need this to proceed with another snippet (which relies on device name in the above mentioned format). Can anyone provide some pointers while I try check out the different ways I could achieve this? I just want to be sure that I am not writing ugly (and dangerous code - crashes, buffer overruns etc). Greatly appreciate any pointers. I guess I am starting to get a little paranoid about handling strings ( especially on low memory devices like typical mcus ).

  • What is the purpose of the following line: uint16_t chip = (uint16_t)(chipid >> 32); ? Thanks Aug 16, 2021 at 19:51
  • @ItamarVieira this copies the highest 16 bits from “chipid” (which is a 48-bit value in a 64-bit variable) to the “chip” variable (which is a 16-bit variable).
    – StarCat
    Aug 17, 2021 at 6:22

3 Answers 3


You can combine all your work into one single line with the snprintf. You aren't limited to just one variable, and you can add plain text:

char ssid[23];

snprintf(ssid, 23, "MCUDEVICE-%04X%08X", chip, (uint32_t)chipid);

Why don't you simply use:

char ssid[23];
snprintf(ssid, 23, "MCUDEVICE-%llX", ESP.getEfuseMac());

This way no need to bit-shift and extra middle variables...

Just watch out for capital X for uppercase result and small 'l' for type indication. (meaning use either llX or llx and not LLX).


This works on an ESP32-S3 WROOM on Arduino 1.8.19:

void setup() {
  // put your setup code here, to run once:

  String MCUtype = "ESP32s3" ;
  char ssid[14];
  snprintf(ssid, 14, "%llX", ESP.getEfuseMac());
  Serial.println(ssid); // 
  String ssid1 = ssid ; // convert from char to String
  int StrLen = ssid1.length();
  Serial.printf("Length of the string %i characters \r\n",StrLen);
  String MAC ="";
  int i = 0; while ( i < 6) {  
    MAC = MAC + ssid1.substring(StrLen-2-2*i,StrLen-2*i)+":";
    i++ ;
  MAC = MAC.substring(0,17); // remove last : 
  String Serialnumber = MCUtype+"-" ;
    int j = 3; while ( j < 6) {  
  Serialnumber = Serialnumber + ssid1.substring(StrLen-2-2*j,StrLen-2*j);
  j++ ;

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


Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.