Noob Q:我有char temp = 128.如何转换“temp”?进入字符串128 \ 0? [英] Noob Q: I have char temp = 128. How do I convert "temp" into string 128\0 ?

查看:113
本文介绍了Noob Q:我有char temp = 128.如何转换“temp”?进入字符串128 \ 0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所示,

我有

As title goes,
I have

char temp = 128// this is the result of a A/D conversion from microcontroller and it could be a number between 0 and 1023.





我正试图通过USART发送这个值,我有两个函数







I''m trying to send this value in temp through USART, which I have both functions


void usart_pstr(char *s) {
    // loop through entire string
    while (*s) {
        usart_putchar(*s);
        s++;
    }
}
void usart_putchar(char data) {
	// Wait for empty transmit buffer
	while ( !(UCSR0A & (1<<UDRE0)) );
	// Start transmission
	UDR0 = data;
}





但是在接收端,串口程序只接受字符串。所以我想知道如何将temp转换为字符串128 \0(而不是ASCII表中的128对应)?



But on the receiving end , the serial port program only takes strings. So I''m wondering how do I convert "temp" into string "128\0" (instead of whatever 128 corresponds to in ASCII table) ?

推荐答案

Quote:

char temp = 128 //这是微控制器进行A / D转换的结果,它可能是0到1023之间的数字。

char temp = 128// this is the result of a A/D conversion from microcontroller and it could be a number between 0 and 1023.



我想你想要




I think you want

char temp[2] = { 128, 0};





但是你的评论有点令人不安。你在这里使用的''char''是签名的,范围从-128到+128。如果你想要一个0到255的范围,使用''unsigned char''。如果你想要一些能给你1023或更多值的东西你可能想要使用Unicode。



Your comment is a little unsettling however. A ''char'' as you have used here is signed and the range goes from -128 to +128. If you want a range from 0 to 255, use ''unsigned char''. If you want something that gives you 1023 or more values you probably want to use Unicode.


从评论中我知道你已经找到 itoa()功能。



但是对于微控制器,内存通常有限,执行时间应尽可能短。因此,自己实现特定功能可能更好,而不是使用更通用的库函数。此示例将生成带有前导零的字符串(例如0128)。

From the comments I know that you already found the itoa() function.

But with microcontrollers there is usually a limited amount of memory and execution time should be as short as possible. So it might be better to implement specific functions yourself rather than using the more general library functions. This example will produce strings with leading zeroes (e.g. ''0128'').
// Convert unsigned decimal number to string with leading zeroes.
// Digits specifies the number of decimal digits to be printed
//  (e.g. 4 for values up to 1023).
// Size of buffer must be at least digits + 1.
// Avoids the usage of the % modulo operator for performance reasons.
char *utoa10(unsigned val, char *buffer, int digits)
{
    unsigned n;
    buffer[digits--] = 0;
    while (digits >= 0)
    {
        n = val / 10;
        buffer[digits--] = (char)(val - n * 10) + '0';
        val = n;
    }
    return buffer;
}

// Send ADC value in the range from 0 to 1023
void usart_adc(unsigned val)
{
    char buffer[5];
    utoa10(val, buffer, 4);
    usart_pstr(buffer);
}


或二进制......

Or in binary...
short Voltage = 128;

usart_putchar(((unsigned char *) &Voltage)[0]);
usart_putchar(((unsigned char *) &Voltage)[1]);





读取它会以类似的方式工作,尽管你需要请注意,您的处理器可以使用不同的字节顺序。所以你可能需要在一侧交换索引。



Reading it back would work in a similar fashion, though you need to be aware that your processors could use different byte ordering. So you may need to swap the indexes on one side.


这篇关于Noob Q:我有char temp = 128.如何转换“temp”?进入字符串128 \ 0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆