How can i convert a uint32_t to a char* type

偶尔善良 提交于 2020-12-05 07:25:46

问题


Hello i am using an Arduino UNO with an adafruit shield to display score values but the function used to display scores only accepts char* values and the score itself can occupy up to 6 digits(000,000 to 999,999). i have tried using sprint() but i have had no luck since the screen will flicker like crazy. i believe the problem to be that chars only hold a certain number of bytes that could not fit a 32 bit int but i would think their is a way around this. draw text is the function used by the shield to drawstuff on the screen with input being char*, color code, size, x pixel, y pixel. if anybody can please help me convert between these two types please let me know. also if their are alternatives that would also help me.

my code:

char* textToWrite;
uint32_t currentScore = 0;
uint32_t highScore = 0;
highScore = currentScore;
sprintf(textToWrite,"%d.%d.%d.%d\0", currentScore);//sprint f not working properly right now
drawText(textToWrite, ST7735_WHITE, 1, 100, 10);

i have also tried using:

sprintf(textToWrite,"%u", currentScore);

回答1:


char textToWrite[ 16 ];
uint32_t currentScore = 42;
// as per comment from LS_dev, platform is int 16bits
sprintf(textToWrite,"%lu", currentScore);


来源:https://stackoverflow.com/questions/20222391/how-can-i-convert-a-uint32-t-to-a-char-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!