Go Down

### Topic: how to convert long int to byte* (Read 13392 times)previous topic - next topic

#### blank

##### Jul 22, 2009, 08:21 pm
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:
Code: [Select]
`append("filename.txt", byte* array,int length)`

when i test
Code: [Select]
`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?

#1

#### Nachtwind

#2
##### Jul 22, 2009, 08:25 pm
Hm, not exactly sure if this is what you want - but maybe this can help you:

Code: [Select]
`long n;byte buf[4];  `

Long -> byte[4]:
Code: [Select]
`         buf[0] = (byte) n;buf[1] = (byte) n >> 8;buf[2] = (byte) n >> 16;buf[3] = (byte) n >> 24;`

byte[4] -> int
Code: [Select]
`long value = (unsigned long)(buf[4] << 24) | (buf[3] << 16) | (buf[2] << 8) | buf[1];`
Believe me, Mike, I calculated the odds of this succeeding against the odds I was doing something incredibly stupid[ch8230] and I went ahead

#3
cheers guys

#### blank

#4
##### Jul 22, 2009, 08:44 pm

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

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

any ideas?

#### AWOL

#5
##### Jul 22, 2009, 09:23 pm
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)
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

#### blank

#6
##### Jul 22, 2009, 09:39 pm
i am getting the same error even if :
Code: [Select]
`char* astring=" ";`
(astring point something before i use it in 'itoa' function)

so its not about undefinition i think !
any other ideas?

#### blank

#7
##### Jul 22, 2009, 09:41 pm
BTW i am getting the same error with Nachtwind's example too ...

#### AlphaBeta

#8
##### Jul 22, 2009, 10:44 pm
Code: [Select]
`char astring[8] = {'\0'};itoa(100,astring,10);`

#### blank

#9
##### Jul 22, 2009, 10:56 pm
same error, this is getting on my nerves

#### Nachtwind

#10
##### Jul 22, 2009, 11:23 pm
if you get that error on both codes with working snippets then the problem is somewhere else - could you give us your whole codefile?
Believe me, Mike, I calculated the odds of this succeeding against the odds I was doing something incredibly stupid[ch8230] and I went ahead

#### blank

#11
##### Jul 22, 2009, 11:48 pmLast Edit: Jul 22, 2009, 11:54 pm by blank Reason: 1
Code: [Select]
`#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);}`

Code: [Select]
`#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.

#### AlphaBeta

#12
##### Jul 23, 2009, 12:36 am
This:
Code: [Select]
`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).

#### blank

#13
##### Jul 23, 2009, 01:30 pmLast Edit: Jul 23, 2009, 01:30 pm by blank Reason: 1
we pulled it off with a floatToString.h function we found in the playground
and reinterpret_cast

Go Up

Please enter a valid email to subscribe