convert byte array[13] to a char

I have a array[13]

byte MyArray[13] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};

And I need to convert that to a char to write it to a SD card I have tried this

file.writeLn((char*)MyArray));

But I get somthing like "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy" And I need it to be "1111111111111" Can somone point me in the right direction? Thanks

You can convert one byte into one char. You can not convert an array of bytes into an a char.

Typically, write() methods write binary data, and print() methods write ASCII data. Since you want ASCII data, you'll need to explain why you are using a write() method.

I have to write the MyArray to "01.TXT" file on the sd card So I can "If the power go's out" write it back to the variable MyArray

I hope this makes it clearer, Thanks

I hope this makes it clearer

No, it doesn't. It really doesn't matter why you want to write the data to the file.

What matters is that you are using the wrong method with the wrong kind of data.

What I am trying to do is take

byte MyArray[13] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};

And write it to the sd card I know what I have done so far is not right but I have no idea what to do next!

I can however get this to work

int varZ = 26;  

har varZChar[10];
        itoa (varZ, varZChar, 10);

and then use varZChar in the

resSD=file.initFAT();
  if (resSD!=NO_ERROR)
  {
    Serial.print("***** ERROR: ");
    Serial.println(verboseError(resSD));
    while (true) {};
  }
  file.openFile(01.TXT, FILEMODE_TEXT_WRITE); 
  file.writeLn(varZChar);

what method should I be looking at? and with what data? Thanks for your help

what method should I be looking at?

The one mentioned in reply #1. Something involving PRINT not WRITE!

When you start using a print() function, you can use the cast that you had.

If I use

file.printLn(varZChar);

I get this error

error: 'class tinyFAT' has no member named 'printLn'

error: ‘class tinyFAT’ has no member named ‘printLn’

You’ve got the source code. Look and see what members it does have!

Or, solve the problem with a sledge hammer:

for(byte i=0; i<13; i++)
{
   file.write((char)(MyArray[i]+'0'));
}
file.writeLn();

Are you trying to get a text file that has "normal" text in it, in this case 11111111 that you could edit with notepad or the like?

If so, try replacing this:

byte MyArray[13] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};

with

byte MyArray[13] = {'1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'};

Sorry for the constant nagging

resSD=file.initFAT();
        file.openFile(08.TXT, FILEMODE_TEXT_WRITE); 
          for(byte i=0; i<13; i++)
          {
            file.writeLn((char)(MyArray[i]+'0'));
          }
          file.writeLn();

gives this error

error: invalid conversion from 'char' to 'char*'

Personally, I’d ditch that library, and use one that DOES know how to write ints and bytes and ASCII data and partial records.

I thank that you are right! What library do you think is best?

What library do you think is best?

The one in Covington. It has more books than the one in Maple Valley. You might have different opinions. It depends on your criteria.

chrisnet: And I need to convert that to a char to write it to a SD card I have tried this

file.writeLn((char*)MyArray));

But I get somthing like "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"

If you look carefully you'll see umlauts/diareses over those "y"s - in my experience that means they are 0xFF bytes, which is what unwritten flash reads as.

Did you flush your writes or close the file properly? I'd expect to see raw 0xFF's if the writes hadn't been flushed out.

chrisnet:
Sorry for the constant nagging

...  {  file.writeLn((char)(MyArray[i]+'0')); ....

gives this error

error: invalid conversion from 'char' to 'char*'

Because you can’t send single char. Parameter for the function is a char* (a string array null terminated ‘\0’)

uint16_t	writeLn(char *st);

TinyFat library not have a function for write single char/byte.
You need to copy your 13 elements from your array to a string (array of char) and after write this variable/array using a single file.writeLn()

char buf[14];
for(byte i=0; i<13; i++)
{  buf[i]=MyArray[i]+'0';   // 1=>'1' and 0=>'0'
}
buf[13]='\0';
file.writeLn(buf);

To put my question a different way, do you really want to see ascii character 1 in your text file, or ascii character 49?

If you don't know what you are doing, I suggest you read the instructions. There are plenty of examples of arduino sketches to all kinds of writing to files. It is not too difficult to figure out what the error messages that you are getting mean.

nid69ita Ill try it as soon as I can! Thanks

nid69ita Thank you it was just what I needed!!!
How would I reverse it and read the file and put it back as ?

byte MyArray[13] = " {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
char buf[14];
for(byte i=0; i<13; i++)
{  buf[i]=MyArray[i]+'0';   // 1=>'1' and 0=>'0'
}
buf[13]='\0';
file.writeLn(buf);
char buf[14];
file.readLn(buf);
if(strlen(buf)==13)
{  for(byte i=0; i<13; i++)
  {  MyArray[i]=buf[i]-'0';   // '1'=>1 and '0'=>0
  }
}

buf can be a unique global variable/array.
And I added a check to be sure you have read 13 chars.

But in array MyArray every value can be only 0 or 1 ? Like a boolean?