我创建了一个简单的程序来随时存储和检索密码。
例如,我有一个密码是Pass
,所以它的二进制文件将是01010000011000010111001101110011
。我创建了一个程序,可以将其转换为二进制文件并将其存储在文件中。程序如下:
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
FILE *fptr;
char wname[100];
char uname[100];
char pass[100];
char str[50];
char bin[7];
int c2n,i,ii;
clrscr();
printf("\nEnter website name: ");
fflush(stdin);
gets(wname);
printf("\nEnter the username: ");
fflush(stdin);
gets(uname);
printf("\nEnter password: ");
fflush(stdin);
gets(pass);
fptr=fopen("DND","a");
printf("Binary of entered string is : ");
fprintf(fptr,"\n");
for(i=0;i<=strlen(wname)-1;i++)
{
c2n=wname[i];
for(ii=7;ii>=0;ii--)
{
bin[ii]=c2n%2;
c2n=c2n/2;
}
for(ii=0;ii<=7;ii++)
{
printf("%d",bin[ii]);
fprintf(fptr,"%d",bin[ii]);
}
}
printf(":");
fprintf(fptr,":");
for(i=0;i<=strlen(uname)-1;i++)
{
c2n=uname[i];
for(ii=7;ii>=0;ii--)
{
bin[ii]=c2n%2;
c2n=c2n/2;
}
for(ii=0;ii<=7;ii++)
{
printf("%d",bin[ii]);
fprintf(fptr,"%d",bin[ii]);
}
}
printf(":");
fprintf(fptr,":");
for(i=0;i<=strlen(pass)-1;i++)
{
c2n=pass[i];
for(ii=7;ii>=0;ii--)
{
bin[ii]=c2n%2;
c2n=c2n/2;
}
for(ii=0;ii<=7;ii++)
{
printf("%d",bin[ii]);
fprintf(fptr,"%d",bin[ii]);
}
}
fclose(fptr);
printf("\n Your website name,user name and password are protected in binary....\nPress any key to close coder......");
getch();
return 0;
}
现在,我想为此创建解码器。我想要一个程序将所有二进制文件转换为一个句子。例如。
01010000011000010111001101110011
=通过
我不知道如何创建它。
最佳答案
这是对我有用的东西。
#include <stdio.h>
int main(int argc, char** argv)
{
int ch1 = 0;
int ch2 = 0;
int count = 0;
// Pass the binary file to be read from as the first argument.
char* file = argv[1];
FILE* in = fopen(file, "r");
if ( in == NULL )
{
printf("Unable to open file %s\n", file);
return 1;
}
while ( (ch1 = fgetc(in)) != EOF )
{
if ( ch1 == '\n' )
{
// Reset counters.
ch2 = 0;
count = 0;
fputc(ch1, stdout);
}
else if ( ch1 == ':' )
{
// Skip it for computing characters.
fputc(ch1, stdout);
}
else
{
ch2 <<= 1;
if ( ch1 == '1' )
{
ch2 += 1;
}
count++;
if ( count == 8 )
{
fputc(ch2, stdout);
count = 0;
}
}
}
fputc('\n', stdout);
fclose(in);
return 0;
}
关于c - 如何将二进制转换为句子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23863033/