system
July 22, 2009, 6:21pm
1
im trying to convert my GPS data from long int, to byte* and save them in my microSD card using the FileLogger library. Im stuck.
FillerLogger uses a function to append the data in a text file
syntax:
append("filename.txt", byte* array,int length)
when i test byte array[]="blah blah blah";
everything works fine
but I want to do the same thing with a long integer variable. How can I do this?
system
July 22, 2009, 6:25pm
2
system
July 22, 2009, 6:25pm
3
Hm, not exactly sure if this is what you want - but maybe this can help you:
long n;
byte buf[4];
Long -> byte[4]:
buf[0] = (byte) n;
buf[1] = (byte) n >> 8;
buf[2] = (byte) n >> 16;
buf[3] = (byte) n >> 24;
byte[4] -> int
long value = (unsigned long)(buf[4] << 24) | (buf[3] << 16) | (buf[2] << 8) | buf[1];
system
July 22, 2009, 6:44pm
5
char* astring;
itoa(100,astring,10);
byte array[]=astring;
and getting the following error: expected constructor, destructor or type conversion before "(" token.
any ideas?
system
July 22, 2009, 7:23pm
6
Well, for a start, "itoa(100, astring, 10)" - at this point "astring" doesn't point at anything, so is undefined, possibly NULL.
This is A Bad Thing (tm)
system
July 22, 2009, 7:39pm
7
i am getting the same error even if :
char* astring=" ";
(astring point something before i use it in 'itoa' function)
so its not about undefinition i think !
any other ideas?
system
July 22, 2009, 7:41pm
8
BTW i am getting the same error with Nachtwind's example too ...
system
July 22, 2009, 8:44pm
9
How about:
char astring[8] = {'\0'};
itoa(100,astring,10);
system
July 22, 2009, 8:56pm
10
same error, this is getting on my nerves
system
July 22, 2009, 9:23pm
11
if you get that error on both codes with working snippets then the problem is somewhere else - could you give us your whole codefile?
system
July 22, 2009, 9:48pm
12
#include "FileLogger.h"
#include <stdlib.h>
#define MEM_PW 8
#define MESSAGE " blah blah \r\n"
unsigned long length = sizeof(MESSAGE)-1;
byte buffer[] = MESSAGE;
char* aString={'\0'};
itoa(100,aString,10);
byte buffer2[] = aString;
unsigned long length2 = sizeof(aString)-1;
int result;
void setup(void)
{
pinMode(MEM_PW, OUTPUT);
digitalWrite(MEM_PW, HIGH);
Serial.begin(115200);
}
void loop(void) {
result = FileLogger::append("data.txt", buffer2, length2);
Serial.print(" Result: ");
if( result == 0)
{
Serial.println("OK");
}
else if( result == 1)
{
Serial.println("Fail initializing");
}
else if( result == 2)
{
Serial.println("Fail appending");
}
delay(500);
result = FileLogger::append("data.txt", buffer, length);
Serial.print(" Result: ");
if( result == 0)
{
Serial.println("OK");
}
else if( result == 1)
{
Serial.println("Fail initializing");
}
else if( result == 2)
{
Serial.println("Fail appending");
}
delay(2000);
}
#include "FileLogger.h"
#include <stdlib.h>
#define MEM_PW 8
#define MESSAGE " blah blah \r\n"
unsigned long length = sizeof(MESSAGE)-1;
unsigned long length2 = sizeof(int);
byte buffer[] = MESSAGE;
byte buffer2[2] ;
int x=1;
void setup(void) {
pinMode(MEM_PW, OUTPUT);
digitalWrite(MEM_PW, HIGH);
Serial.begin(115200);
}
void loop(void) {
int result;
buffer2[0] = byte(x);
buffer2[1] = byte(x>>8);
result = FileLogger::append("data.txt", buffer2, length2);
Serial.print(" Result: ");
if( result == 0)
{
Serial.println("OK");
}
else if( result == 1)
{
Serial.println("Fail initializing");
}
else if( result == 2)
{
Serial.println("Fail appending");
}
delay(500);
result = FileLogger::append("data.txt", buffer, length);
Serial.print(" Result: ");
if( result == 0)
{
Serial.println("OK");
}
else if( result == 1)
{
Serial.println("Fail initializing");
}
else if( result == 2)
{
Serial.println("Fail appending");
}
delay(2000);
++x;
}
i made some changes in the second one, it compiles, but my txt has the square (the one like in binary files) before "blah blah" instead of the x variable.
system
July 22, 2009, 10:36pm
13
This:
itoa(100,aString,10);
Needs to be placed inside the setup .
And this: char* aString={'\0'}; need to be something like: char aString[10]={'\0'}; where 10 is maximum length of integer (decimals).
system
July 23, 2009, 11:30am
14
we pulled it off with a floatToString.h function we found in the playground
and reinterpret_cast