Skip to content

Instantly share code, notes, and snippets.

@DaveCalaway
Last active June 17, 2019 14:17
Show Gist options
  • Save DaveCalaway/f665af7c0746dc560847fab490a060be to your computer and use it in GitHub Desktop.
Save DaveCalaway/f665af7c0746dc560847fab490a060be to your computer and use it in GitHub Desktop.
Long to Bytes - EEPROM
// Thx u internet
/* OUPUT
char: 8800001
to Int: 8800001
1
71
134
0
== 8800001
*/
#include <EEPROM.h>
void setup() {
Serial.begin(9600);
String SMS = "SleepTime 8800001";
unsigned bytes[4]; // 4 byte = 2^32 = 4.294.967.296
SMS.remove(0, 10); // remove SleepTime+space -> remain the time in millisec
Serial.print("char: ");
Serial.println(SMS);
long n = SMS.toInt();
Serial.print("to Int: ");
Serial.println(n);
bytes[3] = n & 0xFF;
EEPROM.write(0, bytes[3]);
Serial.println(bytes[3]);
bytes[2] = (n >> 8) & 0xFF;
EEPROM.write(1, bytes[2]);
Serial.println(bytes[2]);
bytes[1] = (n >> 16) & 0xFF;
EEPROM.write(2, bytes[1]);
Serial.println(bytes[1]);
bytes[0] = (n >> 24) & 0xFF;
EEPROM.write(3, bytes[0]);
Serial.println(bytes[0]);
delay(1000);
//Read the 4 bytes from the eeprom memory.
long four = EEPROM.read(0);
long three = EEPROM.read(1);
long two = EEPROM.read(2);
long one = EEPROM.read(3);
Serial.print("== ");
Serial.println(((four << 0) & 0xFF) + ((three << 8) & 0xFFFF) +
((two << 16) & 0xFFFFFF) + ((one << 24) & 0xFFFFFFFF));
}
void loop() {
// lolololol
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment