Binary to ascii encoder
anirudhvr_at_spymac.com
Date: 12/21/04
- Next message: Keith Thompson: "Re: C call of a C# dll"
- Previous message: Keith Thompson: "Re: strncat copies junk values ...."
- Next in thread: Charlie Gordon: "Re: Binary to ascii encoder"
- Reply: Charlie Gordon: "Re: Binary to ascii encoder"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 21 Dec 2004 02:43:40 -0800
Hi,
I needed a basic binary to ascii encoder, so I wrote this piece of
code:
/*
Encoding algo: suppose 11111010 is the byte to be encoded.
It is first broken up into two 4-bit parts (1111 and 1010)
This 4-bit quantity is then padded between a 01 and a 10, ie,
01xxxx10 will be written out to the output file.
Order: MSBs first, LSBs next
Final output: 01111110, 01101010
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
FILE *in, *out;
char *outfile;
if(argc <= 1)
return EXIT_FAILURE;
if( (in = fopen(argv[1], "r") ) == NULL )
{
printf("File not found\n");
return EXIT_FAILURE;
}
outfile = (char*) malloc( sizeof(char) * ( strlen(argv[1]) + 5 ) );
outfile = strcpy(outfile, argv[1]);
strcat(outfile, ".enc");
if( (out = fopen(outfile, "w") ) == NULL )
{
printf("File could not be opened for writing\n");
return EXIT_FAILURE;
}
while(!feof(in))
{
unsigned char inchar, outchar[2] = {0};
fread(&inchar, 1, 1, in);
outchar[0] = 66 | ( (inchar >> 4) << 2 );
outchar[1] = 66 | ( (unsigned char)(inchar << 4) >> 2 );
fwrite(outchar, 1, 2, out);
}
fclose(in);
fclose(out);
return EXIT_SUCCESS;
}
This, coupled with a similar decoder, works in unix: I'm able to get
the initial binary back after encoding/decoding. However, on a windows
platform, the encoded file is much smaller than the binary used. I
tried with both djgpp and MSVC 6. Is there a non-standard feature that
I've used that might have caused this problem?
Thanks in advance,
Anirudh
- Next message: Keith Thompson: "Re: C call of a C# dll"
- Previous message: Keith Thompson: "Re: strncat copies junk values ...."
- Next in thread: Charlie Gordon: "Re: Binary to ascii encoder"
- Reply: Charlie Gordon: "Re: Binary to ascii encoder"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]