/*
Forum: https://forum.arduino.cc/t/how-to-read-bytes-from-a-rfid-card-and-convert-them-into-an-integer/1158246
Wokwi: https://wokwi.com/projects/373072344139685889
*/
byte buf[8] = {1, 0, 127, 0, 255, 0, 0, 1};
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Start");
Serial.println(IntFromBytes(buf, 0));
Serial.println(IntFromBytes(buf, 2));
Serial.println(IntFromBytes(buf, 4));
Serial.println(IntFromBytes(buf, 6));
writeIntToBytes(20, 0);
writeIntToBytes(40, 2);
writeIntToBytes(120, 4);
writeIntToBytes(512, 6);
Serial.println(IntFromBytes(buf, 0));
Serial.println(IntFromBytes(buf, 2));
Serial.println(IntFromBytes(buf, 4));
Serial.println(IntFromBytes(buf, 6));
}
void loop() {
// put your main code here, to run repeatedly:
}
int IntFromBytes(byte *b, int offset) {
return b[offset] + b[offset + 1] * 256;
}
void writeIntToBytes(int v, int offset) {
buf[offset] = v & 0xFF;
buf[offset + 1] = v >> 8;
}