我正在尝试读取和写入无符号长类型值到NFC卡。
我正在使用pn532模块,MifareUltralight卡和Adafruit库。mifareultralight_WritePage函数似乎只接受 4 字节大小为 uint8_t 类型的数组。
所以我想将一个四字节长类型值一一存储在Uint8_t数组中,并将一个 4 字节长度的Uint8_t数组转换为一个长类型变量。
#include <Adafruit_PN532.h>
...
uint8_t writeBuffer[4] = { 18, 52, 86, 120 };
nfc.mifareultralight_WritePage(8, writeBuffer);
uint8_t readBuffer[4];
success = nfc.mifareultralight_ReadPage(i, &readBuffer[0]);
if (success) {
nfc.PrintHex(readBuffer, 4);
}
in
uint8_t writeBuffer[4] = { 18, 52, 86, 120 };
如果索引 0 包含较低的字节,例如
uint8_t writeBuffer[4] = { 18, 52, 86, 120 };
unsigned long v = writeBuffer[0];
for (size_t i = 1; i != 4; ++i)
v += ((unsigned long) writeBuffer[i]) << (i * 8);
如果索引 0 包含更高的字节,例如
uint8_t writeBuffer[4] = { 18, 52, 86, 120 };
unsigned long v = writeBuffer[3];
for (size_t i = 1; i != 4; ++i)
v += ((unsigned long) writeBuffer[3 - i]) << (i * 8);