本文介绍了将 MFRC522 UID 十六进制字节转换为可打印的十进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在我的 Arduino UNO 上使用 MFRC522 库来读取 Mifare RFID 标签信息.>
I'm using the MFRC522 library on my Arduino UNO to read Mifare RFID tag Info.
// Print HEX UID
Serial.print("Card UID:");
for (byte i = 0; i < mfrc522.uid.size; i++) {
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX);
}
Serial.println();
我有一个包含十六进制 UID 的字节数组 (4):
I've got a byte array(4) which contains the HEX UID:
54 C7 FD 5A
但我未能将其转换为十进制:
But I've failed to convert it to Decimal:
HEX(5AFDC754) => DEC(1526581076)
我尝试将字节数组反向转换为字符,但编译器不让我打印 12 月.
I've tried to convert the byte array to char reversely, but the compiler didn't let me print Dec.
char str[8];
int k = 0;
for (int i = 3; i >= 0 ; i -= 1) {
char hex[4];
snprintf(s, 4, "%x", mfrc522.uid.uidByte[i]);
for( int t = 0; t < 4; t++ ) {
if( (int)hex[t] != 0 )
str[t+k] = hex[t];
}
k+=2;
}
Serial.println( str, DEC);
感谢任何建议
推荐答案
您需要将 4 个十六进制字节组合成一个无符号整数.
You will need to combine the 4 hex bytes into a single unsigned integer.
这取决于 Endianess(搜索它).
This depends on Endianess (search for it).
对于大端:
unsigned int hex_num;
hex_num = uidByte[0] << 24;
hex_num += uidByte[1] << 16;
hex_num += uidByte[2] << 8;
hex_num += uidByte[3];
对于 Little Endian,颠倒 uidByte 位置的顺序.
For Little Endian, reverse the order of uidByte positions.
这篇关于将 MFRC522 UID 十六进制字节转换为可打印的十进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!