文本文档转二进制文档(C语言实现)

 在转换之前首先得了解什么是文件IO,文件IO可分为系统IO和标准IO,本文具体以系统IO实现该功能。

Linux“系统IO”的使用

1.打开文件open

2.操作文件

 想什么呢?自己去百度!直接上代码

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
    int fd = open(const char* pathname,int flags); //文件名 打开方式
    if(-1 == fd) //反过来写的作用不用多说了吧 不懂百度
    {
        perror("open fail");
        return -1;
    }
    printf("open success!\n");
    int fd_dest = open("文件名",O_CREAT|O_EXCL|O_RDWR,0777);
    if(-1 == fd_dest)
    {
        perror("open failed");
        return -1;        
    }
    while(1)
    {
        unsigned char c; 
        unsigned char d = 0;
        for(int i=0;i<2;i++)
        {
            if(read(fd,&c,1)==0)  //每次读写一个字节
            {
                close(fd);
                close(fd_dest);
                return 0;
            } 
            if(c == ' ' || c== '\n' ||c == '\r' ) // c==0xa || c==0xd
            {
                i--;
                continue;
            }
            else if(c >= '0' && c<='9')
            {
                c = c - '0';
            }
            else if(c >= 'a' && c <= 'f')
            {
                c = c - 'a';
                c = c + 10;
            }
            else if(c >= 'A' && c <= 'F')
            {
                c = c - 'A';
                c = c + 10;
            }
            else  //文件中有不符和的字符直接退出
            {
                printf("error char;\n");
                return 0;
            } 
            d <<= 4;
            d |= c; 
        }
        printf("%x ",d);//可加打印调试
        write(fd_dest,&d,1);
    }
    close(fd);
    close(fd_dest);
    return 0;
}

新手写代码,请多改正

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个Python脚本,可以将拖拽进来的文本文件(必须是16进制格式)换为对应的二进制文件: ``` import os import sys # 读取16进制文件内容 def read_hex_file(file_path): with open(file_path, 'rb') as f: hex_data = f.read() return hex_data # 将16进制字符串换为对应的二进制字符串 def hex_to_bin(hex_str): return bin(int(hex_str, 16))[2:] # 将16进制文件换为二进制文件 def hex_to_bin_file(input_file_path, output_file_path): # 读取16进制文件内容 hex_data = read_hex_file(input_file_path) # 将16进制字符串换为二进制字符串 bin_data = ''.join([hex_to_bin(hex_str) for hex_str in hex_data]) # 将二进制字符串换为bytes类型 bin_data_bytes = bytes(int(bin_data[i:i+8], 2) for i in range(0, len(bin_data), 8)) # 写入二进制文件 with open(output_file_path, 'wb') as f: f.write(bin_data_bytes) # 获取拖拽进来的文件路径 if __name__ == '__main__': if len(sys.argv) == 1: print("请拖拽需要换的16进制文件到该脚本上!") else: input_file_path = sys.argv[1] # 16进制文件路径 output_file_path = os.path.splitext(input_file_path)[0] + '.bin' # 输出的二进制文件路径 hex_to_bin_file(input_file_path, output_file_path) print('换完成!') ``` 使用方法: 1. 将以上代码保存为一个.py文件,例如hex2bin_drag.py。 2. 将需要换的16进制文件拖拽到该.py文件上。 3. 执行完毕后,即可在输入的16进制文件所在目录中找到对应的二进制文件。 希望这个脚本能够帮到你!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值