ChrisT
ChrisT

Reputation: 1

How to convert int type number/letter to char and *char?

I am using LittleFS library and ESP32 on arduino IDE. I am reading a file using the example readFile function of LittleFS but I am trying to convert it for my needs. The text written to the file is of this form: LettersAndNumbersMax30&LettersAndNumbersMax30&00&00&01&01

Seperated by &. 2 text values of max 30 characters and 4 integers.

I want to build: char *mytest1 containing the first text char *mytest2 containing the second text int mytest3 containing the first integer (2digits) int mytest4 containing the second integer (2digits) int mytest5 containing the third integer (2digits) int mytest5 containing the forth integer (2digits)

file.read() returns and integer always. for example 38 for &.

void readFile(fs::FS &fs, const char * path){
Serial.printf("Reading file: %s\r\n", path);

File file = fs.open(path);
if(!file || file.isDirectory()){
    Serial.println("- failed to open file for reading");
    return;
}

Serial.println("- read from file:");
while(file.available()){
    Serial.write(file.read());
  
}
file.close();

}

Upvotes: 0

Views: 564

Answers (2)

Michaël Roy
Michaël Roy

Reputation: 6481

Its fairly straightforward. Test each byte read and act accordingly. Code below doesn't handle signs nor negative numbers. It also doesn't check if there are only digits for integers in the file.

#include ....

struct record_t
{
   char myState1[31]; 
   char myState2[31]; 
   int  myState3;
   int  myState4;
   int  myState5;
   int  myState6;
};

record_t record;

bool readFile(fs::FS &fs, const char * path);

void setup()
{
    // ... 
}

void loop()
{
    //...
    if (readFile(/*...*/))
    {
        Serial.printf("file reads OK\r\n");
        //...
    }
}

bool readFile(fs::FS &fs, const char * path)
{
    Serial.printf("Reading file: %s\r\n", path);

    File file = fs.open(path);

    if (!file || file.isDirectory())
    {
        Serial.println("- failed to open file for reading");
        return;
    }

    Serial.println("- read from file:");
    int state = 0;
    int index = 0;

    // clear record.
    record.myState1[0] = 0;
    record.myState2[0] = 0;
    record.myState3 = 0;
    record.myState4 = 0;
    record.myState5 = 0;
    record.myState6 = 0;
 
    bool valid = false;
    for (int i = file.read(); i != -1; i = file.read())
    {
        char c = i & 0xFF; 
        Serial.write(c);    // file.read() returns an int, that's why     Serial.write()
                            //  was printing numbers.

        switch(state)
        {
        case 0:
            if (index > sizeof(record.myState1) - 1) // avoid buffer overflow
                index = sizeof(record.myState1) - 1;
            if (c != '&')
            {
                record.myState1[index++] = c;
            }
            else
            {
                record.myState1[index] = 0;
                ++state;
                index = 0;
            }
            break;

        case 1:
            if (index > sizeof(record.myState2) - 1) // avoid buffer overflow
                index = sizeof(record.myState2) - 1;
            if (c != '&')
            {
                record.myState2[index++] = c;
            }
            else
            {
                record.myState2[index] = 0;
                ++state;
                index = 0;
            }
            break;

        case 2:
            if (c != '&')
                 record.myState3 = record.myState3 * 10 + (c - '0');
            else
                 ++state;
            break;

        case 3:
            if (c != '&')
                record.myState4 = record.myState4 * 10 + (c - '0');
            else
                ++state;
            break;

        case 4:
            if (c != '&')
                record.myState5 = record.myState5 * 10 + (c - '0');
            else
                ++state;
            break;

        case 5:
            valid = true;
            if (c != '&')
                record.myState6 = record.myState6 * 10 + (c - '0');
            else
                ++state;
            break;

        default:  // reaching here is an error condition?  You decide.
            return false;
        }
    }
    file.close();

    if (!valid)
    {
        // clear record.
        record.myState1[0] = 0;
        record.myState2[0] = 0;
        record.myState3 = 0;
        record.myState4 = 0;
        record.myState5 = 0;
        record.myState6 = 0;
    }
    return valid;
}

Upvotes: 1

0___________
0___________

Reputation: 67749

file.read returns integer. So the integer is printed.

You to convert it to the string.

while(file.available()){
    char s[2] = {0};
    s[0] = file.read();
    Serial.write(s);
}

Upvotes: 0

Related Questions