将文件中存储的十进制数转化为2进制

将文件leakyCellFile_32GB_1024ms.txt中的十进制数转化为2进制数,存储在文件HashSourceFile_2中。由于c中没有直接输出2进制的函数,所以选择,首先将文件
leakyCellFile_32GB_1024ms.txt转化为16进制HashSourceFile16.txt,然后读取HashSourceFile16.txt中的16进制数,运用switch,将16进制转化为2进制,写入
HashSourceFile_2中。
/**
*Author:wyj
*Date:2014-03-03
*/
#include <stdio.h>
#include <stdlib.h>
void main(){
FILE *leakyCellInputFile,*testFile;


leakyCellInputFile = fopen("D://leakyCellFile_32GB_1024ms.txt","r");
testFile = fopen("D://HashSourceFile16.txt","w");
int leakyBank,leakyRow,leakyColumnMultiBandWidth;
char buffer[200];


while(fgets(buffer, 50, leakyCellInputFile)){
sscanf(buffer,"%d %d %d",&leakyBank,&leakyRow,&leakyColumnMultiBandWidth);
fprintf(testFile,"%x %x %x\n",leakyBank,leakyRow,leakyColumnMultiBandWidth);
}
fclose(leakyCellInputFile);
fclose(testFile);




leakyCellInputFile = fopen("D://HashSourceFile16.txt","r");
testFile = fopen("D://HashSourceFile_2.txt","w");


int i = 0;
while(fgets(buffer, 50, leakyCellInputFile)){
i = 0;
while(buffer[i]!='\0'){
switch (buffer[i]){
        case '0':fprintf(testFile,"%s","0000");break;
        case '1':fprintf(testFile,"%s","0001");break;
        case '2':fprintf(testFile,"%s","0010");break;
        case '3':fprintf(testFile,"%s","0011");break;
        case '4':fprintf(testFile,"%s","0100");break;
        case '5':fprintf(testFile,"%s","0101");break;
        case '6':fprintf(testFile,"%s","0110");break;
        case '7':fprintf(testFile,"%s","0111");break;
        case '8':fprintf(testFile,"%s","1000");break;
        case '9':fprintf(testFile,"%s","1001");break;
        case 'a':fprintf(testFile,"%s","1010");break;
        case 'b':fprintf(testFile,"%s","1011");break;
        case 'c':fprintf(testFile,"%s","1100");break;
        case 'd':fprintf(testFile,"%s","1101");break;
        case 'e':fprintf(testFile,"%s","1110");break;
        case 'f':fprintf(testFile,"%s","1111");break;
        case '\n':fprintf(testFile,"%c",'\n');break;
       default:fprintf(testFile,"%c",' ');break;
    }
i++;
} 
}
fclose(leakyCellInputFile);
fclose(testFile);

}






  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值