How do I add a prefix 0x to a HEX number read from EEPROM?

Hi can anyone please help me with this. How do I read a number from EEPROM and than add "0x" as a prefix to it? Reading a mac address from EEPROM and need it to display in this format: 0xEEPROM.read(1)

For example I have this:

uint8_t friendmac[LIST_SIZE][ESPPL_MAC_LEN] = {
{0x18, 0x3e, 0xef, 0xc2, 0x57, 0xc7} ,
{0xe2, 0x76, 0x0f, 0x38, 0x7b, 0x3f}
};

This doesn't work since its missing "0x" prefix in front of each read eeprom

uint8_t friendmac[LIST_SIZE][ESPPL_MAC_LEN] = {
{EEPROM.read(1), EEPROM.read(2), EEPROM.read(3), EEPROM.read(4), EEPROM.read(5), EEPROM.read(6)} ,
{EEPROM.read(7), EEPROM.read(8), EEPROM.read(9), EEPROM.read(10), EEPROM.read(11), EEPROM.read(12)}
};

PS in the end I realized that in my code both: decimal OR 0xHEX formats work, so i ended up leaving it as decimal.

Don’t post snippets (Snippets R Us!)

Is that a global variable ?

It’s not the 0x it’s probably because you need to do this with a for loop in your setup

Numbers are numbers. The 0x is only a hexadecimal text representation of the the number. Maybe below code makes it clearer.

void setup()
{
  Serial.begin(57600);

  byte b = 0x34;

  Serial.print("b = ");
  Serial.print(b);
  Serial.println(" in decimal");

  Serial.print("b = 0x");
  Serial.print(b, HEX);

  Serial.println(" in hexadecimal");
}

void loop()
{
}

I meant something like this would do the job
uint8_t friendmac[LIST_SIZE][ESPPL_MAC_LEN];

void setup() {
  for (size_t l = 0; l < LIST_SIZE; l++)
    for (byte m = 0; m < ESPPL_MAC_LEN; m++)
      friendmac[l][m] = EEPROM.read(1 + l * ESPPL_MAC_LEN + m);
}

void loop() {}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.