0. 在openEuler(推荐)或Ubuntu或Windows(不推荐)中完成下面任务,使用git管理代码,git commit 不能少于5次 1. 在附件中的utils.h和utils.c中完成16进制字符'0'-'9','A'-'F','a'-'f'与十六进制数据0-15的转换功能(10’): ``` int Hex2Char(unsigned int fromi,unsigned char * toc); int Char2Hex(unsigned char fromc,unsigned int * toi); ``` 2. 并写出测试代码测试上述函数(不能与下面代码一样),比如:(10’) ``` unsigned char c; Hex2Char(1, &c) //结果:c = '1' Hex2Char(15, &c) //结果:c = 'F' unsigned int i; Char2Hex('1', &i); //结果:i = 1 Char2Hex('F', &i); //结果:i = 15 ``` 3. **提交代码(或代码链接)和运行结果,git log截图**`#ifndef UTILS_H #define UTILS_H
int Hex2Char(unsigned int fromi, unsigned char *toc);
int Char2Hex(unsigned char fromc, unsigned int *toi);
endif // UTILS_H
#include "utils.h"
int Hex2Char(unsigned int fromi, unsigned char *toc) {
if (fromi < 10) {
*toc = '0' + fromi;
} else if (fromi < 16) {
*toc = 'A' + (fromi - 10);
} else {
return -1; // 错误:超出范围
}
return 0; // 成功
}
int Char2Hex(unsigned char fromc, unsigned int *toi) {
if (fromc >= '0' && fromc <= '9') {
*toi = fromc - '0';
} else if (fromc >= 'A' && fromc <= 'F') {
*toi = fromc - 'A' + 10;
} else if (fromc >= 'a' && fromc <= 'f') {
*toi = fromc - 'a' + 10;
} else {
return -1; // 错误:无效字符
}
return 0; // 成功
}
#include <stdio.h>
include "utils.h"
int main() {
unsigned char c;
unsigned int i;
if (Hex2Char(1, &c) == 0) {
printf("Hex2Char(1) -> %c\n", c);
} else {
printf("Hex2Char(1) -> Error\n");
}
if (Hex2Char(15, &c) == 0) {
printf("Hex2Char(15) -> %c\n", c);
} else {
printf("Hex2Char(15) -> Error\n");
}
if (Char2Hex('1', &i) == 0) {
printf("Char2Hex('1') -> %u\n", i);
} else {
printf("Char2Hex('1') -> Error\n");
}
if (Char2Hex('F', &i) == 0) {
printf("Char2Hex('F') -> %u\n", i);
} else {
printf("Char2Hex('F') -> Error\n");
}
return 0;
}
`