Pages: [1]   Go Down
Author Topic: How to write HEX to EEPROM and READ HEX from EEPROM [SOLVED]  (Read 3287 times)
0 Members and 1 Guest are viewing this topic.
Hamme, Belgium
Offline Offline
Sr. Member
****
Karma: 4
Posts: 386
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

the HEX value DE = 222 in DEC
the HEX value AD = 173 in DEC
the HEX value BE = 190 in DEC
the HEX value EF = 239 in DEC
the HEX value FE = 254 in DEC
the HEX value ED = 237 in DEC

I want to write the values "DE" "AD" "BE" "EF" "FE" "ED" to EEPROM. Do have to concert it to DEC first? How to do that and if so, how do I convert it back, when I read the EEPROM. and finally I want to put in to

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};

« Last Edit: August 17, 2011, 10:04:38 am by JO3RI » Logged


Bristol, UK
Offline Offline
Edison Member
*
Karma: 1
Posts: 1197
Exhibitor at UK Maker Faire
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Remember that all variables in the Arduino are simply sored in binary. Writing a number in the source code as hex doesn't change the binary. Printing the same number out as decimal also doesn't change it.  Hex or decimal are just the way we see the number when written or printed out. The EEPROM will store data in binary, too.
Logged

Hamme, Belgium
Offline Offline
Sr. Member
****
Karma: 4
Posts: 386
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#include <EEPROM.h>

int value;

void setup()
{
  Serial.begin(9600);
  EEPROM.write(0,DE);
  EEPROM.write(1,AD);
  EEPROM.write(2,BE);
  EEPROM.write(3,EF);
  EEPROM.write(4,FE);
  EEPROM.write(5,ED);
  value = EEPROM.read(0);
  Serial.println(value,HEX);
  value = EEPROM.read(1);
  Serial.println(value,HEX);
  value = EEPROM.read(2);
  Serial.println(value,HEX);
  value = EEPROM.read(3);
  Serial.println(value,HEX);
  value = EEPROM.read(4);
  Serial.println(value,HEX);
  value = EEPROM.read(5);
  Serial.println(value,HEX);  
}

void loop()
{
}

This doesn't work and I can't find the right code
Logged


Hamme, Belgium
Offline Offline
Sr. Member
****
Karma: 4
Posts: 386
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
EEPROM.write(0,0xED);
  EEPROM.write(1,0xAD);
  EEPROM.write(2,0xBE);
  EEPROM.write(3,0xEF);
  EEPROM.write(4,0xFE);
  EEPROM.write(5,0xED);


Ok this does works, but what if I only get ED from my input and not 0xED, can I do something like this:

int a = 'AD'
int b = '0x'

EEPROM.write(0, a+b)
Logged


Bristol, UK
Offline Offline
Edison Member
*
Karma: 1
Posts: 1197
Exhibitor at UK Maker Faire
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Try this:

Code:
value = EEPROM.read(0);
  Serial.print("0x");
  Serial.println(value,HEX);

Remember, the '0x' is only there in the source code and in the printout. It's not stored in the EEPROM, only the binary representation of ED is (i.e. 11101101).
« Last Edit: August 16, 2011, 11:39:26 am by Anachrocomputer » Logged

Hamme, Belgium
Offline Offline
Sr. Member
****
Karma: 4
Posts: 386
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#include <EEPROM.h>

int value;

void setup()
{
  Serial.begin(9600);
  EEPROM.write(0,0xDE);
  EEPROM.write(1,0xAD);
  EEPROM.write(2,0xBE);
  EEPROM.write(3,0xEF);
  EEPROM.write(4,0xFE);
  EEPROM.write(5,0xED);
  value = EEPROM.read(0);
  Serial.print("0x");
  Serial.println(value,HEX);
  Serial.print("0x");
  value = EEPROM.read(1);
  Serial.println(value,HEX);
  Serial.print("0x");
  value = EEPROM.read(2);
  Serial.println(value,HEX);
  Serial.print("0x");
  value = EEPROM.read(3);
  Serial.println(value,HEX);
  Serial.print("0x");
  value = EEPROM.read(4);
  Serial.println(value,HEX);
  Serial.print("0x");
  value = EEPROM.read(5);
  Serial.println(value,HEX); 
}

void loop()
{
}

Ok, so this works in the Serial monitor but what I really want is that the input "DE" is written to EEPROM (I can't provide "0xDE") This is because I get the value from a FORM submitted via a webpage. Now I realise I have a problem when reading the values from that FORM. I'll have to solve those first and then this problem will be solved too.  smiley-grin

FYI

Code:
TextFinder  finder(client );
    while (client.connected()) {     
      if (client.available()) {
        //hieronder blijven
        if( finder.find("GET /") ) {
          if (finder.findUntil("setup", "\n\r")){
          if (finder.findUntil("SBM", "\n\r")){
            byte SET = finder.getValue();
              while(finder.findUntil("DT", "\n\r")){
                int val = finder.getValue();
                if(val >= 1 && val <= 6) {
                  mac[val - 1] = finder.getValue();
                  Serial.println(finder.getValue());
                }
                if(val >= 7 && val <= 10) {
                  ip[val - 7] = finder.getValue();
                }
                if(val >= 11 && val <= 14) {
                  subnet[val - 11] = finder.getValue();
                }
                if(val >= 15 && val <= 18) {
                  gateway[val - 15] = finder.getValue();
                }
              }
            for (int i = 0 ; i < 6; i++)
            {
              EEPROM.write(i + 1,mac[i]);
            }
            for (int i = 0 ; i < 4; i++)
            {
              EEPROM.write(i + 7, ip[i]);
            }
            for (int i = 0 ; i < 4; i++)
            {
              EEPROM.write(i + 11, subnet[i]);
            }
            for (int i = 0 ; i < 4; i++)
            {
              EEPROM.write(i + 15, gateway[i]);
            }
            //set ID to the known bit, so when you reset the Arduino is will use the EEPROM values
            EEPROM.write(0, 0x99);
            // if al the data has been written to EEPROM we should reset the arduino
            // for now you'll have to use the hardware reset button
          }
Logged


Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
but what I really want is that the input "DE" is written to EEPROM (I can't provide "0xDE") This is because I get the value from a FORM submitted via a webpage. Now I realise I have a problem when reading the values from that FORM. I'll have to solve those first and then this problem will be solved too.
Yes, you will have to write some code to convert a string like "AD" to a byte, so that you can write it to EEPROM.

It really isn't that difficult. Extract each character. If the character is between '0' and '9', subtract '0' from it. If the character is between 'A' and 'F', subtract 'A' and add 10. If the character is between 'a' and 'f', subtract 'a' and add 10.

If the input is "AD", the characters are 'A' and 'D'. The values that you end up with are 10 and 13. Multiply the first by 16 and add the second. 10 * 16 + 13 = 173. 173 is equal to 0xAD.
Logged

Hamme, Belgium
Offline Offline
Sr. Member
****
Karma: 4
Posts: 386
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi PaulS,

As you might know, my actual problem (you can read about that, here: http://arduino.cc/forum/index.php/topic,69600.0.html)
was finder.getValue() that couldn't cope with HEX. So i found a way around the problem with some javascript that converts HEX to DEC and that my friend, I CAN write to EEPROM. So this problem has been solved too
Logged


Pages: [1]   Go Up
Jump to: