Hi all,
First, thank you in advance for your help!
I read different answers on this forum about reading bytes from a .txt file stored in an SD but I still haven't understood which would be the better solution for my problem.
I have to read lines of a long .txt file from an SD using an Arduino Mega 2560.
I get that what I imagined to be just one character in each row is actually a sequence of bytes that I would need to save and use the atoi function to recunstract it.
Is there a fast instruction or library to read the number before the "." character in one byte without doing this process?
I would need to read and save in one byte each line ( number before the '.') very fastly and send in real time the byte received to a Shift Register. Time is an important requirement for my code.
How do I have to change my text file in order to avoid to loose time reading unnecessary char like the ones after the "."
It would be better to change my .txt files in an ASCII characters sequence in order to have the correspondent ASCII value to that number? Is there an easy way to do that?
I am sorry to ask again a question on this topic but reading different answer I was just more confused.
In my world, a text file is a bunch of ASCII characters followed by a CR/LF. Carriage return and line feed. Periods, commas, dollar signs, other characters do not matter. IT is the CR/LF that ends a line.
How is your text file created? A word processor, hex editor, etc?
Taking the first line of text "131.00000000". The number "131" that you want is actually three bytes. It is the ASCII character "1", then the ASCII character "3", then the ASCII character "1".
What you want is to store the value 131, not the text representation of it. It won't be a ".TXT" file at that point, and should probably be labeled ".DAT". Like Paul asked...How was the file created? If you know Python you could write a script to convert the file.
I created the .txt file in Matlab. Your suggestion, if I am not wrong is that of modify my .txt file and create a .dat file in which I save my array as a sequence of "uint8" values. Correct? I will dry to find something on how to do that.
Just a question came to me..
Once that, for example, my value 255 is saved as "11111111" in the file. The Arduino file.read() is able to read the binary number as 255 and store it in a single byte, and not as a sequence of 8 bytes representing 1. Right?
But I am a little bit confused.. I was using an online convertitor from ASCII values to text and I've seen that only certain values are correctly decoded, as 85 or 3..
The ascii characters correspondent to other don't give me the correct number back:
For example, my ASCII rapresentation for 131 is ƒ.. When I go again to a number representation I got:
198 146
Instead of 131..
I was wondering if someone, for sure more prepared on data conversion and data types than me, knows what is happening here..
LauraBentivoglio:
Just a question came to me..
Once that, for example, my value 255 is saved as "11111111" in the file. The Arduino file.read() is able to read the binary number as 255 and store it in a single byte, and not as a sequence of 8 bytes representing 1. Right?
You are right. But it's best not to think of the value 255 being stored as '11111111'. Only think of the value stored, which is 255. And data is stored on the SD card as individual bytes. As long as you write it as a byte you will get the right number.
The confusion, maybe, is that you might think "131.00000000" is a number. It is not. It is a bunch of ASCII characters. Twelve bytes in all. When you use a pen to write the number "72", you are not writing seventy-two things. You are writing two characters that you interpret as meaning the number 72.