极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 18936|回复: 9

【求助】数组转换为16进制字符串

[复制链接]
发表于 2016-4-27 00:21:00 | 显示全部楼层 |阅读模式
本帖最后由 maxims 于 2016-4-28 17:22 编辑

  1. /**
  2. * Helper routine to dump a byte array as hex values to Serial.
  3. */
  4. void printHex(byte *buffer, byte bufferSize) {
  5.   for (byte i = 0; i < bufferSize; i++) {
  6.     Serial.print(buffer[i] < 0x10 ? " 0" : " ");
  7.     Serial.print(buffer[i], HEX);
  8.   }
  9. }
复制代码


Rc522的卡号,读出来是数组格式,一般用上面的方法进行输出为字符串。
但是,我在程序中需要引用它。通常是如下引用

  1. if (rfid.uid.uidByte[0] != nuidPICC[0] ||
  2.     rfid.uid.uidByte[1] != nuidPICC[1] ||
  3.     rfid.uid.uidByte[2] != nuidPICC[2] ||
  4.     rfid.uid.uidByte[3] != nuidPICC[3] ) {
  5.     Serial.println(F("A new card has been detected."));
复制代码


但是,我只想要16进制的字符串引用,该如何转换呢?
比如,我需要直接输出到1602,它就必须是"xxxxxxxx"这种【16进制字符串】


这是RC522的Demo
  1. #include <SPI.h>
  2. #include <MFRC522.h>

  3. #define SS_PIN 10
  4. #define RST_PIN 9

  5. MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class

  6. MFRC522::MIFARE_Key key;

  7. // Init array that will store new NUID
  8. byte nuidPICC[3];

  9. void setup() {
  10.   Serial.begin(9600);
  11.   SPI.begin(); // Init SPI bus
  12.   rfid.PCD_Init(); // Init MFRC522

  13.   for (byte i = 0; i < 6; i++) {
  14.     key.keyByte[i] = 0xFF;
  15.   }

  16.   Serial.println(F("This code scan the MIFARE Classsic NUID."));
  17.   Serial.print(F("Using the following key:"));
  18.   printHex(key.keyByte, MFRC522::MF_KEY_SIZE);
  19. }

  20. void loop() {

  21.   // Look for new cards
  22.   if ( ! rfid.PICC_IsNewCardPresent())
  23.     return;

  24.   // Verify if the NUID has been readed
  25.   if ( ! rfid.PICC_ReadCardSerial())
  26.     return;

  27.   Serial.print(F("PICC type: "));
  28.   MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
  29.   Serial.println(rfid.PICC_GetTypeName(piccType));

  30.   // Check is the PICC of Classic MIFARE type
  31.   if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&  
  32.     piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
  33.     piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
  34.     Serial.println(F("Your tag is not of type MIFARE Classic."));
  35.     return;
  36.   }

  37.   if (rfid.uid.uidByte[0] != nuidPICC[0] ||
  38.     rfid.uid.uidByte[1] != nuidPICC[1] ||
  39.     rfid.uid.uidByte[2] != nuidPICC[2] ||
  40.     rfid.uid.uidByte[3] != nuidPICC[3] ) {
  41.     Serial.println(F("A new card has been detected."));

  42.     // Store NUID into nuidPICC array
  43.     for (byte i = 0; i < 4; i++) {
  44.       nuidPICC[i] = rfid.uid.uidByte[i];
  45.     }
  46.    
  47.     Serial.println(F("The NUID tag is:"));
  48.     Serial.print(F("In hex: "));
  49.     printHex(rfid.uid.uidByte, rfid.uid.size);
  50.     Serial.println();
  51.     Serial.print(F("In dec: "));
  52.     printDec(rfid.uid.uidByte, rfid.uid.size);
  53.     Serial.println();
  54.   }
  55.   else Serial.println(F("Card read previously."));

  56.   // Halt PICC
  57.   rfid.PICC_HaltA();

  58.   // Stop encryption on PCD
  59.   rfid.PCD_StopCrypto1();
  60. }


  61. /**
  62. * Helper routine to dump a byte array as hex values to Serial.
  63. */
  64. void printHex(byte *buffer, byte bufferSize) {
  65.   for (byte i = 0; i < bufferSize; i++) {
  66.     Serial.print(buffer[i] < 0x10 ? " 0" : " ");
  67.     Serial.print(buffer[i], HEX);
  68.   }
  69. }

  70. /**
  71. * Helper routine to dump a byte array as dec values to Serial.
  72. */
  73. void printDec(byte *buffer, byte bufferSize) {
  74.   for (byte i = 0; i < bufferSize; i++) {
  75.     Serial.print(buffer[i] < 0x10 ? " 0" : " ");
  76.     Serial.print(buffer[i], DEC);
  77.   }
  78. }
复制代码
回复

使用道具 举报

 楼主| 发表于 2016-4-27 23:25:03 | 显示全部楼层
又没人理我。。。。难道我额问题太弱智?
回复 支持 反对

使用道具 举报

发表于 2016-4-28 10:10:26 | 显示全部楼层
sprintf(buffer,"%X%X%X%X",nuid[0],nuid[1],nuid[2],nuid[3]);
回复 支持 反对

使用道具 举报

发表于 2016-4-28 15:40:22 | 显示全部楼层
maxims 发表于 2016-4-27 23:25
又没人理我。。。。难道我额问题太弱智?

你不是太弱智同,是含糊不清。您倒是给几个有代表性、有概括性的,输入和输出的样例啊。另外你所说的字符串的“引用”是什么意思?是C++的“引用变量”还是别的什么?不要发明新名词好不好。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-4-28 17:11:49 | 显示全部楼层
cjnt007 发表于 2016-4-28 10:10
sprintf(buffer,"%X%X%X%X",nuid[0],nuid[1],nuid[2],nuid[3]);

额,好像有那么点道理,回头我试试~先谢了。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-4-28 17:21:47 | 显示全部楼层
GDHack 发表于 2016-4-28 15:40
你不是太弱智同,是含糊不清。您倒是给几个有代表性、有概括性的,输入和输出的样例啊。另外你所说的字符 ...

好吧~
先看第一段代码,它是把byte *buffer用下标进行访问并输出到串口。串口软件上收到的就是一个连续的字符串了。

byte *buffer实际上是把rfid.uid.uidByte的内容通过函数调用传递过去的。
假设:
rfid.uid.uidByte[6] = {'A','B','1','2','3','4'}//请先别管这个语句是否成立与否,我只是表示它内容大概如此。
我可以通过
  1. void printHex(byte *buffer, byte bufferSize) {
  2.   for (byte i = 0; i < bufferSize; i++) {
  3.     Serial.print(buffer[i] < 0x10 ? " 0" : " ");
  4.     Serial.print(buffer[i], HEX);
  5.   }
  6. }
复制代码
把他输出到串口,得到AB1234。

这是串口软件中得到的。
假设,我在程序里边需要lcd.print("hello, world!");明显"hello, world!"它是个字符串,直接lcd.print(rfid.uid.uidByte);肯定是不行的。又如:Serial.print(rfid.uid.uidByte+"is my name");它也是不行的。我只能"AB1234"+"is my name"。

问题:我如何才能方便的把这个rfid.uid.uidByte当成字符串来使用(引用?)
回复 支持 反对

使用道具 举报

发表于 2016-4-29 16:16:41 | 显示全部楼层
maxims 发表于 2016-4-28 17:21
好吧~
先看第一段代码,它是把byte *buffer用下标进行访问并输出到串口。串口软件上收到的就是一个连续的 ...

其实你真正的问题是:如何将BYTE数组变量转化成String。
这样喽:

  1. void setup() {
  2.   Serial.begin(9600);
  3.   char b[] = {'L', 'y', 'l', 'e', ' ', 'C', 'h', 'e', 'n'};
  4.   String s;
  5.   s = String(b);
  6.   Serial.println("I am " + s);
  7.   Serial.println("I am " + s);
  8.   Serial.println("I am " + s);
  9.   Serial.println("Important words repeated for three!");
  10. }

  11. void loop() {}
复制代码

不过中文字符似乎会显示出错
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-4-29 21:34:20 | 显示全部楼层
GDHack 发表于 2016-4-29 16:16
其实你真正的问题是:如何将BYTE数组变量转化成String。
这样喽:

你这个代码,我一楼不是有了?
你总结的问题是对的,但答案却不是我要的。
回复 支持 反对

使用道具 举报

发表于 2016-4-30 14:57:18 | 显示全部楼层
maxims 发表于 2016-4-29 21:34
你这个代码,我一楼不是有了?
你总结的问题是对的,但答案却不是我要的。

哪里有!我贴的代码是指用“String(array)”来建立String对象。你不是想要“方便的把这个rfid.uid.uidByte当成字符串来使用”么?这不是有String了?
看来我是帮不了你了……
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-4-30 20:47:48 | 显示全部楼层
GDHack 发表于 2016-4-30 14:57
哪里有!我贴的代码是指用“String(array)”来建立String对象。你不是想要“方便的把这个rfid.uid.uidByt ...

嗷,Sorry,我漏看了,String(b)这句了。我还以为是用Serial输出哪些呢。
谢谢~
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-3-28 22:49 , Processed in 0.044516 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表