how to convert long int to byte*

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?

Convert the integer to a string.
http://www.cplusplus.com/reference/clibrary/cstdlib/itoa/ :slight_smile:

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];

cheers guys

char* astring;
itoa(100,astring,10);
byte array[]=astring;

and getting the following error: expected constructor, destructor or type conversion before "(" token.

any ideas?

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)

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?

BTW i am getting the same error with Nachtwind's example too ...

How about:

char astring[8] = {'\0'};
itoa(100,astring,10);

same error, this is getting on my nerves

if you get that error on both codes with working snippets then the problem is somewhere else - could you give us your whole codefile?

#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.

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).

we pulled it off with a floatToString.h function we found in the playground
and reinterpret_cast :slight_smile: