/* 0-9 减去0x30 A-F 减去0x37
你需要如下转换
char str[2] ={5,6};
5的字是0x35,你char test[0] = char[0] - 0x30
test[0] = test[0] <<4 |(char[1]-0x30)
这样56就可以放到一个字节里面成为0x56了
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int removeWhitespace(char* str) {
int i, j;
for (i = 0, j = 0; str[i] != '\0'; i++) {
if (str[i] != ' ' && str[i] != '\t' && str[i] != '\n') {
str[j++] = str[i];
}
}
str[j] = '\0';
return j;
}
#if 0 //去掉空格
int main() {
FILE* file = fopen("input.txt", "r+");
FILE* file1 = fopen("input_rm_space.txt", "w");
int length_file1 = 0;
if (file == NULL) {
printf("Failed to open the file.\n");
return 1;
}
fseek(file, 0, SEEK_END);
long fileSize = ftell(file);
fseek(file, 0, SEEK_SET);
char* content = (char*)malloc(fileSize + 1);
if (content == NULL) {
printf("Failed to allocate memory.\n");
fclose(file);
return 1;
}
fread(content, 1, fileSize, file);
content[fileSize] = '\0';
fclose(file);
length_file1 = removeWhitespace(content);
fwrite(content, 1, length_file1, file1);
//printf("Result: %s\n", content);
free(content);
fclose(file1);
return 0;
}
#endif
#if 1//处理 去掉空格的文件
int main() {
FILE *inputFile, *outputFile;
char inputFileName[] = "input_rm_space.txt";
char outputFileName[] = "output.txt";
char byte[3];
char tmp;
int count = 0;
// 初始化累加值为0x00000000
unsigned int sum = 0x00000000;
// 定义一个字符串来存储结果
char result[12]; // 8位数字 + 1个'h' + 3个结束符'\0'
// 打开input.txt文件
inputFile = fopen(inputFileName, "r");
if (inputFile == NULL) {
printf("无法打开文件 %s\n", inputFileName);
return 1;
}
// 创建output.txt文件
outputFile = fopen(outputFileName, "w");
if (outputFile == NULL) {
printf("无法创建文件 %s\n", outputFileName);
return 1;
}
// 逐行读取input.txt文件内容并处理
while (!feof(inputFile)) {
// 读取一行数据
//fgets(address, sizeof(address), inputFile);
// 处理第一部分:地址 ,将结果转换为字符串,并存入到result中
//sprintf(result, "%08xh", sum);
//fprintf(outputFile, "%s: ", result);
// 处理第二部分:字节
while (count < 16) {
fgets(byte, sizeof(byte), inputFile);
if(byte[0] > 0x40) byte[0] = byte[0] - 0x37;
else byte[0] = byte[0] - 0x30;
if(byte[1] > 0x40) byte[1] = byte[1] - 0x37;
else byte[1] = byte[1] - 0x30;
tmp = (byte[0]<<4)|byte[1];
fprintf(outputFile, "%c", tmp);
count++;
}
sum += 0x10;
// 处理完一行后换行
//fprintf(outputFile, "\n");
// 重置计数器
count = 0;
}
// 关闭文件
fclose(inputFile);
fclose(outputFile);
printf("处理完成!请查看 %s 文件。\n", outputFileName);
return 0;
}
#endif
/* 0-9 减去0x30 A-F 减去0x37
你需要如下转换
char str[2] ={5,6};
5的字是0x35,你char test[0] = char[0] - 0x30
test[0] = test[0] <<4 |(char[1]-0x30)
这样56就可以放到一个字节里面成为0x56了
*/
#if 0 //十六进制的累加
int main() {
// 初始化累加值为0x00000000
unsigned int sum = 0x00000000;
// 循环10次,每次累加0x10
for (int i = 0; i < 10; i++) {
sum += 0x10;
}
// 定义一个字符串来存储结果
char result[12]; // 8位数字 + 1个'h' + 2个结束符'\0'
// 将结果转换为字符串,并存入到result中
sprintf(result, "%08xh", sum);
// 输出结果
printf("Sum: %s\n", result);
return 0;
}
#endif
#if 0
int main() {
FILE *file = fopen("output.hex", "wb"); // 打开一个名为"output.hex"的文件以写入数据
if (file == NULL) {
printf("无法打开文件\n");
return 1;
}
char buffer[2];
// 写入一些十六进制数据到文件
fprintf(file, "%02X",0x56);
// sprintf(buffer, "%02X", 0x56); // 将字符转换为十六进制表示并存储在buffer中
// fputs(buffer, file); // 将十六进制数据写入新文件
fclose(file); // 关闭文件
return 0;
}
#endif
#if 0
int main(int argc,char *argv[])
{
char *strTest = "I am OK!don't worry.";
int strLen=strlen(strTest);
int i=0;
FILE *fp;
if((fp=fopen("test.txt","wb")) == NULL)
{
printf("file open failed!");
return 0;
}
printf("%d\n",strLen);
while(i<strLen)
{
fprintf(fp,"%02x ",strTest[i++]);
}
return 1;
}
#endif