简体   繁体   English

Arduino unsigned long int to char*

[英]Arduino unsigned long int to char*

I am coding on Attiny85 with IRremote and SSD1306 libraries customized.我正在使用定制的 IRremote 和 SSD1306 库在 Attiny85 上编码。

When receiving IR data the results are stored this way:接收 IR 数据时,结果以这种方式存储:

unsigned long                 value     // Decoded value, max 32 bits
volatile unsigned int         *rawbuf   // Raw interval in 50uS ticks
int                           rawlen    // Number of records in rawbuf

OLED display related functions are: OLED显示相关功能有:

void printChar                (char ch)
void printString              (char* pText)

I am struggling with printing the received IR value to the OLED SSD1306 screen.我正在努力将接收到的 IR 值打印到 OLED SSD1306 屏幕。 User is able to review and save the received code to EEPROM and hence I have tried all different conversions but I am unable to print the results.value to the screen in a way that it would display the received HEX code (for example 0xE0E040BF which is Power on in Samsung television).用户能够查看接收到的代码并将其保存到 EEPROM,因此我尝试了所有不同的转换,但我无法以显示接收到的十六进制代码的方式将 results.value 打印到屏幕上(例如 0xE0E040BF,它是在三星电视上开机)。

Due to lag of Serial in attiny85 I have no clue how I could debug this and get it working.由于 Serial 在 attiny85 中的滞后,我不知道如何调试它并使其工作。 Any help?有什么帮助吗?

EDIT (adding relative code):编辑(添加相关代码):

#include "SSD1306.h"
#include "IRremote.h"
...
if (irrecv.decode(&results)) {  
    dumpCode(&results);        
    irsend.sendRaw(buff, results.rawlen, 38);
    oled.printString("Received IR");
    // Print received hexadecimal IR code to the OLED screen
    irrecv.enableIRIn();    
    irrecv.resume();   
}

If I understand it correctly you just want to convert a unsigned long value into an hex string, you can use sprintf for this purpose ( ideone ):如果我理解正确,您只想将unsigned long值转换为十六进制字符串,您可以为此使用sprintf ( ideone ):

const unsigned int BUFFER_LENGTH = 16;
char buffer[BUFFER_LENGTH];
unsigned long value = 0xE0E040BF;
sprintf(buffer, "0x%08X", value);
printf("%s\n", buffer); // printString(buffer)

so that you can pass buffer to the printString method of the oled screen.以便您可以将buffer传递给 oled 屏幕的printString方法。

The format specifier %08X instructs printf to format the value as an hexadecimal number, with capital letters always showing all 8 hex values for the 4 bytes and padding it with 0s.格式说明符%08X指示 printf 将值格式化为十六进制数,大写字母始终显示 4 个字节的所有 8 个十六进制值,并用 0 填充它。

You could do this:你可以这样做:

int IR;
char hexadec_s[11];
sprintf(hexadec_s,"0x%08x",IR);
printString(hexadec_s);

I believe this should do the trick.我相信这应该可以解决问题。 Let me know if it worked.让我知道它是否有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM