Strunggling with reading from SD-card

Hi Community,

I am trying to decode the text file in my sd-card. So first 3 bytes should be converted into one hex value and the rest of bytes should be converted into 8 hex values. PLEASE NOTE: the last byte "N" is an error, so dont count it.

/*
  SD card read/write

 This example shows how to read and write data to and from an SD card file
 The circuit:
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)

 created   Nov 2010
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe

 This example code is in the public domain.

 */

#include <SPI.h>
#include <SD.h>
int i ;
File myFile;
void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  Serial.print("Initializing SD card...");

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");

  myFile = SD.open("test2.txt");
  i=0;


  
}


void loop() {
  
  // re-open the file for reading:
  if (myFile) {
    
    // read from the file until there's nothing else in it:
    if (myFile.available()  ) {
      String list = myFile.readStringUntil("\n");
      Serial.println(list);
      }
     
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
  }

Above is my code and attached you can find the captured serial monitor. So the first line

"385100100000255" should be 0x181 and 0x0A 0x00 0x0A 0X00 0x00 0x00 0x00 0xFF.

So I tried to extract the first three bytes :

void loop() {
  
  // re-open the file for reading:
  if (myFile) {
    
    // read from the file until there's nothing else in it:
    if (myFile.available()  ) {
      String list = myFile.readStringUntil("\n");
      Serial.print(list[0]);
      Serial.print(list[1]);
      Serial.print(list[2]);
      }

But the output is just : 385
Why doesnt this code go through line by line, but stops at the first line ?

serial.PNG

there are more lines in the file? use while (file.available())

It looks like your code is doing what you tell it to but it sounds like you want to convert characters to numbers. The below code may give you something to think about.

BTW, it's better to use char instead of String.

void setup()
{

  Serial.begin(115200);
  String list = "385100100000255";
  Serial.print("list = ");
  Serial.println(list);

  // convert first 3 digits to int
  int val = (list[0] - '0') * 100 + (list[1] - '0') * 10 + (list[2] - '0');

  Serial.print("val = ");
  Serial.println(val);              // val = 385

  Serial.print("hex val = 0x");
  Serial.println(val, HEX);         // hex val = 0x181
  
}

void loop ()
{
 
}

Juraj:
there are more lines in the file? use while (file.available())

void loop() {
  
    while (myFile.available()  ) {
      String list = myFile.readStringUntil("\n");
      Serial.print(list[0]);
      Serial.print(list[1]);
      Serial.print(list[2]);
      }

No using while() still reads only 385. Besides since this code is in the loop(), it does not actually matter whether if(myFile.available()) or while(myFile.available()) is used, right?

what is in the file? the first code in your post prints more lines?

Yes, my very first code prints more than one line. Please have a look at the attached file.

add the file as attachment. I will try it