gpt4 book ai didi

c - GNU C 中 float 的 printf 实现,半主机

转载 作者:行者123 更新时间:2023-11-30 17:54:54 24 4
gpt4 key购买 nike

我需要使用 gnu c printf 函数将 float 发送到半主机控制台。当前的实现 printf(vsnprintf) 是

 signed int vsnprintf(char *pStr, size_t length, const char *pFormat, va_list ap)
{
char fill;
unsigned char width;
signed int num = 0;
signed int size = 0;

/* Clear the string */
if (pStr) {

*pStr = 0;
}

/* Phase string */
while (*pFormat != 0 && size < length) {

/* Normal character */
if (*pFormat != '%') {

*pStr++ = *pFormat++;
size++;
}
/* Escaped '%' */
else if (*(pFormat+1) == '%') {

*pStr++ = '%';
pFormat += 2;
size++;
}
/* Token delimiter */
else {

fill = ' ';
width = 0;
pFormat++;

/* Parse filler */
if (*pFormat == '0') {

fill = '0';
pFormat++;
}

/* Parse width */
while ((*pFormat >= '0') && (*pFormat <= '9')) {

width = (width*10) + *pFormat-'0';
pFormat++;
}

/* Check if there is enough space */
if (size + width > length) {

width = length - size;
}

/* Parse type */
switch (*pFormat) {
case 'd':
case 'i': num = PutSignedInt(pStr, fill, width, va_arg(ap, signed int)); break;
case 'u': num = PutUnsignedInt(pStr, fill, width, va_arg(ap, unsigned int)); break;
case 'x': num = PutHexa(pStr, fill, width, 0, va_arg(ap, unsigned int)); break;
case 'X': num = PutHexa(pStr, fill, width, 1, va_arg(ap, unsigned int)); break;
case 's': num = PutString(pStr, va_arg(ap, char *)); break;
case 'c': num = PutChar(pStr, va_arg(ap, unsigned int)); break;
default:
return EOF;
}

pFormat++;
pStr += num;
size += num;
}
}

/* NULL-terminated (final \0 is not counted) */
if (size < length) {

*pStr = 0;
}
else {

*(--pStr) = 0;
size--;
}

return size;
}

非常感谢任何实现“f”格式说明符的帮助

最佳答案

您似乎正在使用自定义 printf 实现,而不是使用 libc 工具链中的实现。假设您已实现 syscalls ,您应该能够通过不在 stdio 实现中进行编译来简单地切换到工具链的标准 printf 实现。

另一种方法可能是创建一个 PutFloat 函数,该函数只需将输入乘以 10 的幂,然后使用现有整数打印分别打印数字的上方和下方小数部分。例如:

x = (signed int)floatIn*10000;
PutSignedInt(x/10000);
PutChar('.');
ax = abs(x);
ay = abs(y);
ax = ax - ay*10000;
PutSignedInt(ax);

如果您明白了,您应该能够自己填写详细信息。

关于c - GNU C 中 float 的 printf 实现,半主机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14674036/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com