keil uvision2实现被转换的ASCII码十进制数45、12(即34H,35H,31H,32H)存放在0500H~0503H单元中,转换的结果存放在20H,21H单元中。
时间: 2023-12-10 21:02:34 浏览: 107
实现步骤如下:
1. 定义变量并初始化
```
unsigned char ascii[4] = {0x34, 0x35, 0x31, 0x32}; // 被转换的ASCII码
unsigned char result[2] = {0}; // 转换结果,初始值为0
```
2. 进行ASCII码到十进制数的转换
```
result[0] = (ascii[0] - 0x30) * 10 + (ascii[1] - 0x30); // 转换十位数
result[1] = (ascii[2] - 0x30) * 10 + (ascii[3] - 0x30); // 转换个位数
```
3. 存储转换结果
```
*(unsigned char*)0x0500 = ascii[0]; // 存储被转换的ASCII码
*(unsigned char*)0x0501 = ascii[1];
*(unsigned char*)0x0502 = ascii[2];
*(unsigned char*)0x0503 = ascii[3];
*(unsigned char*)0x0020 = result[0]; // 存储转换结果
*(unsigned char*)0x0021 = result[1];
```
完整代码如下:
```
void main()
{
unsigned char ascii[4] = {0x34, 0x35, 0x31, 0x32}; // 被转换的ASCII码
unsigned char result[2] = {0}; // 转换结果,初始值为0
result[0] = (ascii[0] - 0x30) * 10 + (ascii[1] - 0x30); // 转换十位数
result[1] = (ascii[2] - 0x30) * 10 + (ascii[3] - 0x30); // 转换个位数
*(unsigned char*)0x0500 = ascii[0]; // 存储被转换的ASCII码
*(unsigned char*)0x0501 = ascii[1];
*(unsigned char*)0x0502 = ascii[2];
*(unsigned char*)0x0503 = ascii[3];
*(unsigned char*)0x0020 = result[0]; // 存储转换结果
*(unsigned char*)0x0021 = result[1];
}
```
阅读全文