我正在尝试将无符号长型值写入NFC卡。

我正在使用pn532模块,MifareUltralight卡和Adafruit库。 mifareultralight_WritePage函数似乎仅接受uint8_t类型的数组,其大小为4字节。

因此,我想将一个四字节长类型的值一个接一个地存储在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);
}

最佳答案




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

08-16 11:53