在C编程中将Fputc二进制转换为ASCII [英] Fputc binary to ascii in C programming
本文介绍了在C编程中将Fputc二进制转换为ASCII的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨朋友
我有一个用c语言编写的文件,该文件包含二进制字节,意味着只有0和1!
我想将每8个字节收集为ASCII并保存在另一个文件中!
谢谢
我尝试过的事情:
bit_files * tmp;
tmp =(bit_files *)malloc( sizeof (bit_files));
字符 a;
FILE * first_file,* final_file;
first_file = fopen(" ," r");
final_file = fopen(" ," w");
tmp-> bitBuffer<< = 1 ;
tmp-> bitCount = 0 ;
while (a = fgetc(first_file)!= EOF)
{
如果(a == 0 )
{
tmp-> bitBuffer = tmp-> bitBuffer<< = 1 ;
}
其他
{
tmp-> bitBuffer = tmp-> bitBuffer | = 1 ;
}
tmp-> bitCount ++;
如果(tmp-> bitCount == 8 )
{
fputc(tmp-> bitBuffer,final_file);
tmp-> bitBuffer = 0 ;
tmp-> bitCount = 0 ;
}
}
fclose(first_file);
fclose(final_file);
解决方案
二进制文件"不只包含零和1,还包含二进制数据-未签名的char每个值包含0到255之间(含0和255)的值.如果您的数据确实是每个字节仅二进制值0和1,那么您所要做的就是使用<<循环遍历无符号字符中的每个八位.运算符,将每个位移到适当位置,然后在完成后将每个组合字节写入输出中.
但是我首先要使用十六进制编辑器查看您的二进制文件,以确保您不仅误解了整个应用程序!
一种更简单的方法是读取八个字节一次并从低阶位组合ascii,例如:
char inputBuffer [ 8 ]; 字符 temp; FILE * binin; int i; binin = fopen(" ," rb"); 如果(binin == NULL) exit( 1 ); 做 { int nIn = fread(inputBuffer, sizeof ( char ), sizeof inputBuffer,binin); 如果(nIn< sizeof inputBuffer) break ; temp = 0 ; for (i = 0 ; i< 8 ; ++ i) { temp | =(inputBuffer [i]& 1 )<< ( 7 -i); } printf(" ,临时); } 同时( 1 ); printf(" );
>
hi friends
i have a file in c langage , this file contatins binary byte it means only 0 and one !
i want to collect every 8 bytes to ASCII and save in another file !
thank you
What I have tried:
bit_files *tmp;
tmp = (bit_files *)malloc(sizeof(bit_files));
char a;
FILE *first_file,*final_file;
first_file=fopen("file1","r");
final_file=fopen("file2","w");
tmp->bitBuffer <<= 1;
tmp->bitCount = 0;
while (a=fgetc(first_file)!=EOF)
{
if (a==0)
{
tmp->bitBuffer=tmp->bitBuffer<<=1;
}
else
{
tmp->bitBuffer=tmp->bitBuffer|=1;
}
tmp->bitCount++;
if (tmp->bitCount==8)
{
fputc(tmp->bitBuffer,final_file);
tmp->bitBuffer=0;
tmp->bitCount=0;
}
}
fclose(first_file);
fclose(final_file);
解决方案
A "binary file" doesn''t contain just zero and 1, it contains binary data - which are unsigned char values each containing between 0 and 255 inclusive. If your data truly is the binary values 0 and 1 only per byte, then all you need to do is loop through collecting each eight bits in an unsigned char by using the << operator to move each bit up into place and then write each assembled byte to your output when it''s finished.
But I''d start by looking at your binary file with a hex editor to make sure you aren''t just misthinking the whole app!
A simpler way would be to read eight bytes at a time and assemble the ascii from the low order bits, something like:
char inputBuffer[8]; char temp; FILE* binin; int i; binin = fopen("ctest.txt", "rb"); if (binin == NULL) exit(1); do { int nIn = fread(inputBuffer, sizeof(char), sizeof inputBuffer, binin); if (nIn < sizeof inputBuffer) break; temp = 0; for (i = 0; i < 8; ++i) { temp |= (inputBuffer[i] & 1) << (7 - i); } printf("%c", temp); } while(1); printf("\n");
这篇关于在C编程中将Fputc二进制转换为ASCII的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文