简体   繁体   English

如何将字符更改为 ASCII 形式?

[英]How to change a char to ASCII form?

I need change my program, how do I use some function change a char to ASCII?我需要更改我的程序,如何使用某些函数将字符更改为 ASCII? my mobile receive data from 8051, always show '0', it's not true.我的手机从 8051 接收数据,总是显示“0”,这不是真的。

char to ASCII code.字符到 ASCII 码。

void Data_TX(unsigned char Y)
{
    unsigned char Buff_Y[3];

    Buff_Y[2] = (Y / 100) + 0x30;
    Buff_Y[1] = (Y / 10) % 10+0x30;
    Buff_Y[0] = (Y % 10) + 0x30;

    SBUF = *Buff_Y;
    while (TI == 0);
    TI = 0;
} 

this's my circuit original code, LCD interfacing with 8051.这是我的电路原始代码,LCD 与 8051 接口。

bit Sensor_read(unsigned char read[5]);

use my function.使用我的功能。

Data_TX(read[2]); //read data.
Data_TX((int)read[2]); //read data, this program can't to run. 
Buff_Y[2] = (Y / 100) + 0x30;
Buff_Y[1] = (Y / 10) % 10+0x30;
Buff_Y[0] = (Y % 10) + 0x30;

This is all good.这一切都很好。 If you print those three bytes, you will see that they contain the right values.如果您打印这三个字节,您将看到它们包含正确的值。


SBUF = *Buff_Y;

This, however, indicates confusion.然而,这表明混乱。 *Buff_Y is equivalent to Buff_Y[0] . *Buff_Y相当于Buff_Y[0] That is a single char value... If you can only store a single char value into SBUF , then you can't store the three char values you want to store into it.那是单个char值...如果您只能将单个char值存储到SBUF ,那么您就无法将要存储的三个char值存储到其中。

If we use logic, we can make this inference: That char value will be '0' if the input you provide is evenly divisible by 10 (that is, when Y % 10 is 0 )...如果我们使用逻辑,我们可以做出这样的推断:如果您提供的输入可以被 10 整除(即,当Y % 100 ),则该char值将为'0' ...

While you're thinking about how to change this line of code (and the parts of your code that we can't see), you might also want to think carefully about while (TI == 0);当您考虑如何更改这行代码(以及我们看不到的代码部分)时,您可能还想仔细考虑while (TI == 0); . . There ought to be a better solution to whatever problem that is attempting to solve.对于试图解决的任何问题,都应该有更好的解决方案。

I'm change this code, can be run, but just received digit in ones.我正在更改此代码,可以运行,但只是收到了数字。

void Data_TX(unsigned char Y)

{ unsigned char Buff_Y[3]; { 无符号字符 Buff_Y[3];

Buff_Y[3] = (Y / 100) + 0x30;
Buff_Y[2] = (Y%100)/10+0x30;
Buff_Y[1] = (Y / 10) % 10+0x30;
Buff_Y[0] = (Y % 10) + 0x30;

SBUF = *Buff_Y;
while (TI == 0);
TI = 0;

} }

Data_TX((int)read[2]);

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

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