Converting a double to a binary array

Hello everyone,
I recently got myself a GPS module for the arduino, using the tinygps++ library I can extract the gps data from the module and print it in the serial monitor. I want to be able to store the gps data in a binary array so I can later use the array to turn on and off a digital pin (which i have all sorted out). Now the problem is that I have no idea how to convert the double type (from tinygps++) to a binary array.

Example: 51809562 (received from tinygps type: double) to

myarray[] = {1,1,0,0,0,1,0,1,1,0,1,0,0,0,1,1,0,1,0,0,0,1,1,0,1,0}

I hope someone know how to handle this situation.
Thanks,
Gustav

http://arduiniana.org/libraries/tinygpsplus/

Example: 51809562 (received from tinygps type: double)

Are you sure that it is a double ?
It looks more like an unsigned long to me

Simple data types are basically all binary arrays, it is just a matter of casting.

double value = 123.456;
byte* ptr = (byte*)&value;
for (byte i = 0; i < sizeof(double); i++) Serial.println(ptr[i]);

Danois90:
Simple data types are basically all binary arrays, it is just a matter of casting.

double value = 123.456;

byte* ptr = (byte*)&value;
for (byte i = 0; i < sizeof(double); i++) Serial.println(ptr[i]);

Alright I tried the code and I sort of understand it but I still don't know how to put this data into an actual array.
I also don't exactly understand the output on the serial monitor, for example if I set the double value to 255 the output would be (converted to bin):

0
0
1111111
1000011

I see that 11111111 equals the input (255) and that there are a total of 4 bytes (size of a double) but where does the 1000011 come from?

Which data item from TinyGPS++ is the value coming from ?

UKHeliBob:
Which data item from TinyGPS++ is the value coming from ?

(gps.location.lat(), 6); // Latitude in degrees (double)

Sorry i forgot to add a decimal point in the post so it should be: 51.809562. (I originally wanted to multiply the number by a million to get rid of the decimal point)

gustavlassche:
but I still don't know how to put this data into an actual array.

"ptr" is a pointer accessed as an actual array. If what you want is to convert the data to an array of bytes/chars with the value limited to "0" and "1" then I would advice against it. It is a waste of space and there are better methods of storing the values.

The values in the serial monitor are the bytes used to store the floating point value in memory.

Will 51809562 received as a double be held in memory in the same format as if it were an integer as your proposed conversion to myarray[] = {1,1,0,0,0,1,0,1,1,0,1,0,0,0,1,1,0,1,0,0,0,1,1,0,1,0} implies ?

I actually got it to work on my own, I probably didnt formulate my question the right way. Anyway, this is what I meant:

long val = 51809562;
int b = 0;
int lenght = 26;
int arr1[26];
void setup() {
  Serial.begin(9600);

}

void loop() {
for(int i = 0; i < lenght; i++){
  b = bitRead(val, i);
  arr1[i] = b;
}
 for(int i = 0; i <lenght; i++)
         {
          Serial.print(arr1[i]);
          }
          Serial.println(" ");
}

This code actually reverses the number into the array but in my case thats no problem.

gustavlassche:
I actually got it to work on my own,

Your code uses 52 bytes to store the information that could be stored in 4 bytes.

That's assuming an AVR processor like on an Uno or Mega. On a 32-bit ARM or ESP, it would be 104 bytes.

To call that implementation "wasteful" or "inefficient" would be an understatement.

gustavlassche:
This code actually reverses the number into the array but in my case thats no problem.

Your problem is that you waste over 48 bytes of memory on nothing.

long val = 51809562;

I see that the double has magically become a long

Okay okay, I know this isn't the right way to do this but I have no idea how to do this in a more efficiënt way... I need to be able to make an array consisting out of ones and zeroes, send that array element by element to another arduino and then recreate the decimal number.

What is the problem in just sending the 4 bytes in the long, it is still binary..

Serial.write(&long_value, sizeof(long_value));

EDIT: If this is a school project, you could also just do "bitRead" on the fly and send the bits as bytes one by one on the fly instead of converting them into an array:

for (byte i = 0; i < 32; i++)
{
  byte b = bitRead(long_value, i);
  Serial.write(b);
}

Danois90:
What is the problem in just sending the 4 bytes in the long, it is still binary..

Serial.write(&long_value, sizeof(long_value));

EDIT: If this is a school project, you could also just do "bitRead" on the fly and send the bits as bytes one by one on the fly instead of converting them into an array:

for (byte i = 0; i < 32; i++)

{
  byte b = bitRead(long_value, i);
  Serial.write(b);
}

I'm actually doing a little project where I want to send data over to another arduino using a laser pointer and an LDR, the laser pointer can be on, representing a 1 or off, representing a 0. In order to turn on and off the laser i figured I would use an array to store the on and off states. Does my approach make sense in this way or am i still wasting wasting space on the arduino?

Does my approach make sense in this way or am i still wasting wasting space on the arduino?

You are still wasting space. Just read the bits from the variable when you want to send them. No need for an array