[英]Convert volatile int to int8_t for output
I am working with existing code and am trying to not butcher the code that outputs text on a touchscreen display. 我正在使用现有代码,并试图不保留在触摸屏上输出文本的代码。 The text is defined as
int8_t
and will not allow me to combine text with integer. 文本定义为
int8_t
,不允许我将文本与整数组合。 I am doing this on a TI launchpad MSP432 with booster pack K350QVG. 我在带有增强包K350QVG的TI启动板MSP432上执行此操作。 I have done multiple searches on this site and Google but can't get code to work that others have suggested and would like some help and explanation please.
我已经在该网站和Google上进行了多次搜索,但无法获得他人建议的代码,请提供帮助和解释。
Some code I am working with: 我正在使用的一些代码:
Graphics_drawStringCentered(&g_sContext, "Draw Rectangles",
AUTO_STRING_LENGTH, 159, 15, TRANSPARENT_TEXT);
The "Draw Rectangles" I would like to change to "Value equals: " + Value 我想将“绘制矩形”更改为“值等于:” +值
void Graphics_drawStringCentered(const Graphics_Context *context,
int8_t *string, int32_t length, int32_t x, int32_t y,
bool opaque)
{
Graphics_drawString(context, string, length,
(x) - (Graphics_getStringWidth(context, string, length) / 2),
(y) - (context->font->baseline / 2), opaque);
}
When I try to add it, I get this error 当我尝试添加它时,出现此错误
#
169-D argument of type " char *
" is incompatible with parameter of type " int8_t *
"* #
169-D类型为char *
参数与类型为int8_t *
“ *的参数不兼容 I have tried several methods of converting an int
to int8_t
but have not found anything that works. 我尝试了几种将
int
转换为int8_t
方法,但没有找到任何有效的方法。 Can you please help suggest what to try and I will post my results. 能否请您提出建议尝试一下,我将发布结果。
It sounds like you are trying to concatenate strings using the "+" operator. 听起来您正在尝试使用“ +”运算符连接字符串。 You cannot use the "+" operator to concatenate strings in C. Instead, you have to allocate the memory for the new string yourself, and then you can use the standard library function strncat() from string.h to concatenate the strings.
您不能使用“ +”运算符来连接C中的字符串。相反,您必须自己为新字符串分配内存,然后可以使用string.h中的标准库函数strncat()来连接字符串。
The second issue is the use of int8_t* instead of char* for a C string. 第二个问题是对C字符串使用int8_t *而不是char *。 That is not a standard type for strings in C, and I don't know why the existing code uses it.
那不是C语言中字符串的标准类型,我也不知道为什么现有代码使用它。 However, if you are just using ASCII characters, then casting when calling Graphics_drawStringCentered() should work.
但是,如果仅使用ASCII字符,则在调用Graphics_drawStringCentered()时进行强制转换应该可以工作。
#include <string.h>
int8_t* Value = (int8_t*)"123"; /* string using a strange type */
char theString[256]; /* create a 256-byte buffer to hold the string */
strncpy(theString, "Value: ", 256); /* initialize the buffer with the first string */
strncat(theString, (char*)Value, 256); /* append the second string */
theString[255] = '\0'; /* ensure the string is NULL-terminated */
Graphics_drawStringCentered(&g_sContext, (int8_t*)theString,
AUTO_STRING_LENGTH, 159, 15, TRANSPARENT_TEXT);
Graphics_drawString()
does not need the string buffer to persist after it returns. Graphics_drawString()
返回后不需要持久保留字符串缓冲区。 strncpy()
and strncat()
. strncpy()
和strncat()
构建字符串。 If your logic allows you to build the whole string at once, you can use snprintf(theString, 256, "Value: %s", (char*)Value);
snprintf(theString, 256, "Value: %s", (char*)Value);
as a simpler alternative. strncpy()
and strncat()
, snprintf()
will always NULL-terminate the string.) strncpy()
和strncat()
, snprintf()
始终以NULL终止字符串。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.