Hi,
I am on a Yun, so I use FileIO class.
When I open a file which is located on the SD card, using FileSystem.open(), I read decimal and not ASCII characters. >:(
How can I read / convert ASCII values ?
Thanks
Hi,
I am on a Yun, so I use FileIO class.
When I open a file which is located on the SD card, using FileSystem.open(), I read decimal and not ASCII characters. >:(
How can I read / convert ASCII values ?
Thanks
Can you post your code? I imagine you can do this:
// get file
File myfile = FileSystem.open("myfile.txt");
//Check if this didn't work
if (!myfile) {
print("This is broken");
exit;
}
// read a byte
char my_char = myfile.read();
file.read() is defined to return an int. If you use a numeric datatype variable to read into, you'll get an integer ascii code. If you use a char datatype, it will convert it from ascii to a character for you.
File thefile = FileSystem.open( "thefile.txt");
if( thefile )
{ int d_char = thefile.read(); // <-- d_char will be a integer ascii code.
byte b_char = thefile.read(); // <-- b_char will be a integer ascii code.
long l_char = thefile.read(); // <-- l_char will be a integer ascii code.
char c_char = thefile.read(); // <-- c_char will be a character.
}
Also you should be able to convert between the two using these:
char AsciiToChar( byte asciiCode ) { return (asciiCode +'\0' ); }
byte CharToAscii( char character ) { return (character -'\0' ); }
Or you can use a simple cast:
File thefile = FileSystem.open( "thefile.txt");
if( thefile )
{ int d_char = thefile.read(); // <-- d_char will be a integer ascii code.
byte b_char = thefile.read(); // <-- b_char will be a integer ascii code.
long l_char = thefile.read(); // <-- l_char will be a integer ascii code.
char c_char = thefile.read(); // <-- c_char will be a character.
// Every type here can be cast and printed as a char:
Serial.println( (char) d);
Serial.println( (char) b);
Serial.println( (char) l);
Serial.println( c);
}
yup.