Want read data from EEPROM as HEX, not DEC

Hi, I want ask community for solution of my problem. I have saved values as HEX in EEPROM, but I didnt find way, how to get it as HEX, everytime was translated as DEC. Please, kan you kick me, where I do mistake? Thanx.

#include <EEPROM.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32


const char DD[] PROGMEM = "ADVANCING";
const char DE[] PROGMEM = "AUTOMATIC";
const char DF[] PROGMEM = "BACKWARD";
const char EA[] PROGMEM = "BEFORE";
const char EB[] PROGMEM = "BEGINING";
const char EC[] PROGMEM = "CLOSE";
const char EF[] PROGMEM = "CONNECTED";
const char FA[] PROGMEM = "COOLING";
const char FD[] PROGMEM = "DISABLED";
const char A9[] PROGMEM = "EJECTOR";
const char BA[] PROGMEM = "ENABLE";
const char BB[] PROGMEM = "ENABLED";
const char BC[] PROGMEM = "FORWARD";
const char D9[] PROGMEM = "MANUAL";


int address = 0x00;
byte value;

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

void setup() {
  Wire.begin();
  Wire.setClock(1000000UL);
  Serial.begin(115200);
  if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;);
  }
  delay(100);
  display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0, 0);
  display.println(F("WELCOME"));
  display.display();
  
  Serial.print("EEPROM.length = "); Serial.println(EEPROM.length());
  delay(2000);
}

char datas=0x00;
byte data=0x00;
char myChar;
char cb[16];
byte i;
char hex[50];



void tisk(int dis){
  dis = (dis*5);
  Serial.print("dis = ");Serial.println(dis);

    char buf[4];
    sprintf(buf, "%X", (EEPROM.read(dis + 0)));
   Serial.print(" data = ");Serial.println(buf);
   
    for (byte k = 0; k < strlen_P(buf); k++) {
    myChar = pgm_read_byte_near(buf + k);
    Serial.print(" k=");Serial.print(k);Serial.print(" mychar=");Serial.print(myChar);
    }
}   


/*    EEPROM.get(dis + 1, data);
    for (byte k = 0; k < strlen_P((data)); k++) {
    char myChar = pgm_read_byte_near((data) + k);
    Serial.print(myChar);
    }
    EEPROM.get(dis + 2, data);
    for (byte k = 0; k < strlen_P((data)); k++) {
    char myChar = pgm_read_byte_near((data) + k);
    Serial.print(myChar);
    }
    EEPROM.get(dis + 3, data);
    for (byte k = 0; k < strlen_P((data, HEX)); k++) {
    char myChar = pgm_read_byte_near((data, HEX) + k);
    Serial.print(myChar);
    }
    EEPROM.get(dis + 4, data);
    for (byte k = 0; k < strlen_P((data, HEX)); k++) {
    char myChar = pgm_read_byte_near((data, HEX) + k);
    Serial.print(myChar);
    }
 */   

/*
void SendOnDisplay(int dis) {
  display.stopscroll();
  display.clearDisplay();
  display.setCursor(0, 0);
  display.print((const __FlashStringHelper *)rules[dis][0]);
  display.setCursor(0, 15);
  display.print((const __FlashStringHelper *)rules[dis][1]);
  display.setCursor(0, 31);
  display.print((const __FlashStringHelper *)rules[dis][2]);
  display.setCursor(0, 48);
  char message[10];
  snprintf(message, sizeof(message), "%S", (rules[dis][4]));
  if (strcmp("R", message) == 0) {
    display.startscrollright(0x06, 0x0F);
  }
  if (strcmp("L", message) == 0) {
    display.startscrollleft(0x06, 0x0F);
  }
  display.print((const __FlashStringHelper *)rules[dis][3]);
  display.display();
  delay(4000);  // wait time for show message
}
*/

void loop() {
for (int z=0; z < 38; z++) {
  tisk(z);delay(100);
//  SendOnDisplay(z);delay(1000);
}

delay(1000);
}

If I fill manually to line "for (byte k = 0; k < strlen_P(buf); k++) {`" - buf as direct HEX i.e. DD, AD, DF, A9, then it did working. So, main problem I have, that script read from EEPROM and interpret saved HEX code as DEC.

All data is saved to EEPROM as bytes, it is how you interpret them that makes them decimal, hex, binary etc

Are you asking how to save data to EEPROM as a string (lowercase s) that represents the data as hex and then read the string back and convert it to decimal ?

1 Like

In the EEPROM I have next content, F7B9EFCBCEBEC9DECECEBEC9D9A8CEF7.....
And I want read by byte, as F7 - B9 - EF - CB - CE - BE.....
But, I didnt find way, how to get it as HEX format. Reading return DEC format as 247 - 185 - 239 - 203...
I mean, the problem is, how to read as HEX, because when I put straight HEX symbol, like F7, then it did continue as I wanted.

What is saving the data to EEPROM in that format ?

I have saved shortcuts to PROGMEM adress, const char DD[] PROGMEM = "ADVANCING"; const char DE[] PROGMEM = "AUTOMATIC"; const char DF[] PROGMEM = "BACKWARD"; const char EA[] PROGMEM = "BEFORE";"
And I want by reading from EEPROM sequence, which saved text in PROGMEM, will be send to display, I want make a sentences from five bytes - address. F7 - first word, B9 - second word, ....

I don't understand why you are writing anything to the EEPROM as HEX

I specifying what I think would be the solution for me.

  1. [code] EEPROM.get(dis + 1, data);
    for (byte k = 0; k < strlen_P((data)); k++) {
    char myChar = pgm_read_byte_near((data) + k);
    Serial.print(myChar);
    } [/code ]
    Here script read "data" from eeprom as decimal, so result is bad, if I will read here as hexa, it will be working perfect, if I did put instead variable "data" - direct symbols "F9" - then it did continue as I want.
  2. Or, how to achieve, that output which I did read from eeprom, will be HEX and not DEC as I get now.

I did write shortcuts to EEPROM, because I have only 1kB SRAM, and have not space for OLED RAM.

Otherwise, when I will use avr with 4kB SRAM, everything is OK. But I have at home about 10pcs. ATmega32, and I want use it...:slight_smile:

Sorry, but I am still not getting why you would choose to write a Hex representation of a number to EEPROM

The reason is to move data from sram to eeprom, due to small ram size.
If you have any idea, how to save to eeprom as decimal, I will glad, when you explain me.

EEPROM.put(destination, value);

value can be an integer for example.

https://www.arduino.cc/en/Reference/EEPROMPut

Please post a small but complete sketch showing what you are saving to EEPROM and how you are doing it

and by the way, if SRAM is small, use the F-Makro whenever you can. You left a lot of outputs without...

 Serial.print("EEPROM.length = ")

read here at the bottom of the page:
https://www.arduino.cc/reference/en/language/variables/utilities/progmem/

I did saved to EEPROM by external editor, content I did explain in communication above.
CRC and another about is ok. If I will read directly from each addressess and then print as HEXC, it is everything correct. But as I wrote, I don't know how to get as HEX and then put to strlen_P function. If use serial.print (blbla, HEX) it did display same values as I have saved in eeprom.

Of course, not enough, thanx.

@MiroSmet : That is the point you obviously don't understand. In the end everything you store - whether eprom, sram or flash is stored as a binary pattern. The difference is in the end who you interpret it when printing it e.g. with Serial.print.

Yes, correct, saved as BIN, and I would ask, how to get as HEX :slight_smile:

I am going to try again

Why are you saving the data as HEX in the first place ?

Content of my eeprom, I did made by external editor, I dont need chnge anymore.

:20000000F7B9EFCBCEBEC9DECECEBEC9D9A8CEF7A6BAE9CEB8F8B7FDCEC8EEE8DACFB8ECA6
:20002000B7BBCEBDFAE8DCCEBFC7CACCCEF7A6BAF9CEB8EDB7FDCEC8A7E8DBC6B8F8B7BBEE
:20004000CEF7A6BAFFCEA9DFB7FDCEA9DDE8AECFA9BCB7BBCEF7A6BAFECEA9BCB7FDCEA961
:20006000E7E8AFC6A9D7B7BBCEF7B6FBBCCEFBDFB7FDCEFBDDE8AACFFBBCB7BBCEF7B6FC15
:20008000BCCEFCDFB7FDCEFCDDE8ACCFFCBCB7BBCEF7B6FCDFCEFCBCB7FDCEFCE7E8ADC677
:2000A000FCDFB7BBCEF7B6FBDFCEFBBCB7FDCEFBE7E8ABC6FBDFB7BBCECDEAD8EBCE0000FA
:2000C000000000000000000000000000000000000000000000000000000000000000000020
:2000E000000000000000000000000000000000000000000000000000000000000000000000