Pages: [1]   Go Down
Author Topic: how to convert long int to byte*  (Read 4653 times)
0 Members and 1 Guest are viewing this topic.
Greece
Offline Offline
Jr. Member
**
Karma: 0
Posts: 75
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
append("filename.txt", byte* array,int length)

when i test
Code:
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?

Logged

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 12
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Bonn, Germany
Offline Offline
God Member
*****
Karma: 0
Posts: 903
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hm, not exactly sure if this is what you want - but maybe this can help you:

Code:
long n;
byte buf[4];  

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

byte[4] -> int
Code:
long value = (unsigned long)(buf[4] << 24) | (buf[3] << 16) | (buf[2] << 8) | buf[1];
Logged

Believe me, Mike, I calculated the odds of this succeeding against the odds I was doing something incredibly stupid[ch8230] and I went ahead

Greece
Offline Offline
Jr. Member
**
Karma: 0
Posts: 75
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

cheers guys
« Last Edit: July 22, 2009, 01:35:12 pm by blank » Logged

Greece
Offline Offline
Jr. Member
**
Karma: 0
Posts: 75
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset



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

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

any ideas?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24284
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Greece
Offline Offline
Jr. Member
**
Karma: 0
Posts: 75
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i am getting the same error even if :
Code:
char* astring=" ";
(astring point something before i use it in 'itoa' function)

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

Greece
Offline Offline
Jr. Member
**
Karma: 0
Posts: 75
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 12
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

How about:
Code:
char astring[8] = {'\0'};
itoa(100,astring,10);
Logged

Greece
Offline Offline
Jr. Member
**
Karma: 0
Posts: 75
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

same error, this is getting on my nerves
Logged

Bonn, Germany
Offline Offline
God Member
*****
Karma: 0
Posts: 903
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Believe me, Mike, I calculated the odds of this succeeding against the odds I was doing something incredibly stupid[ch8230] and I went ahead

Greece
Offline Offline
Jr. Member
**
Karma: 0
Posts: 75
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#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:
#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.
« Last Edit: July 22, 2009, 04:54:47 pm by blank » Logged

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 12
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

This:
Code:
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).
Logged

Greece
Offline Offline
Jr. Member
**
Karma: 0
Posts: 75
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

we pulled it off with a floatToString.h function we found in the playground
and reinterpret_cast  smiley
« Last Edit: July 23, 2009, 06:30:56 am by blank » Logged

Pages: [1]   Go Up
Jump to: