十六进制数字56转换成0x56,并写入文件

/* 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

  • 9
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值