118 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			C
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			118 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			C
		
	
	
		
			Executable File
		
	
	
	
	
#include <stdio.h>
 | 
						|
#include <stdint.h>
 | 
						|
#include <unistd.h>
 | 
						|
#include <fcntl.h>
 | 
						|
#include <string.h>
 | 
						|
#include <malloc.h>
 | 
						|
#include <stdlib.h>
 | 
						|
#include <ctype.h>
 | 
						|
 | 
						|
#define strtoul(cp, endp, base)    simple_strtoul(cp, endp, base)
 | 
						|
 | 
						|
void print_binary(FILE *fp, uint32_t n)
 | 
						|
{
 | 
						|
    uint8_t arr[32] = {0};
 | 
						|
    int len = 0;
 | 
						|
    for(len = 0; len < 32; len++){
 | 
						|
        arr[len] = (n&1)+'0';
 | 
						|
        n = n>>1;
 | 
						|
 | 
						|
    }
 | 
						|
 | 
						|
    len--;
 | 
						|
    while (len >= 0 ){
 | 
						|
        fprintf(fp,"%c", arr[len]);
 | 
						|
        len--;
 | 
						|
    }
 | 
						|
    fprintf(fp,"\n");
 | 
						|
}
 | 
						|
 | 
						|
unsigned long simple_strtoul(const char *cp, char **endp,
 | 
						|
                unsigned int base)
 | 
						|
{
 | 
						|
    unsigned long result = 0;
 | 
						|
    unsigned long value;
 | 
						|
 | 
						|
    if (*cp == '0') {
 | 
						|
        cp++;
 | 
						|
        if ((*cp == 'x') && isxdigit(cp[1])) {
 | 
						|
            base = 16;
 | 
						|
            cp++;
 | 
						|
        }
 | 
						|
 | 
						|
        if (!base)
 | 
						|
            base = 8;
 | 
						|
    }
 | 
						|
 | 
						|
    if (!base)
 | 
						|
        base = 10;
 | 
						|
 | 
						|
    while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
 | 
						|
        ? toupper(*cp) : *cp)-'A'+10) < base) {
 | 
						|
        result = result*base + value;
 | 
						|
        cp++;
 | 
						|
    }
 | 
						|
 | 
						|
    if (endp)
 | 
						|
        *endp = (char *)cp;
 | 
						|
 | 
						|
    return result;
 | 
						|
}
 | 
						|
 | 
						|
int main( int argc, char *argv[] )
 | 
						|
{
 | 
						|
    char buf[128];
 | 
						|
    FILE *fin = NULL, *fout = NULL;
 | 
						|
    int i = 0;
 | 
						|
    uint8_t val;
 | 
						|
    off_t filesize, offset;
 | 
						|
    uint32_t tmp = 0;
 | 
						|
 | 
						|
    if( ( fin = fopen( argv[1], "rb" ) ) == NULL ){
 | 
						|
        printf("fopen(%s,r) failed\n", argv[1] );
 | 
						|
        goto exit;
 | 
						|
    }
 | 
						|
 | 
						|
    if( ( fout = fopen( argv[2], "w+" ) ) == NULL ){
 | 
						|
        printf("fopen(%s,w+) failed\n", argv[2] );
 | 
						|
        goto exit;
 | 
						|
    }
 | 
						|
 | 
						|
    if( argc > 3){
 | 
						|
        filesize = atoi(argv[3]);
 | 
						|
    } else{
 | 
						|
        if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 ){
 | 
						|
            perror( "lseek" );
 | 
						|
            goto exit;
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    if( fseek( fin, 0, SEEK_SET ) < 0 ){
 | 
						|
         printf( "fseek(0,SEEK_SET) failed\n" );
 | 
						|
         goto exit;
 | 
						|
    }
 | 
						|
 | 
						|
    while(1) {
 | 
						|
        memset(buf, 0x0, 128);
 | 
						|
        char *st = fgets(buf,128, fin);
 | 
						|
        char **endp = NULL;
 | 
						|
 | 
						|
        if (st == NULL) {
 | 
						|
            break;
 | 
						|
        }
 | 
						|
        tmp = strtoul(buf, endp, 16);
 | 
						|
        //printf("%s:%08x\n", buf, tmp);
 | 
						|
        //fprintf(fout, "%08x\n", tmp);
 | 
						|
        print_binary(fout, tmp);
 | 
						|
    }
 | 
						|
 | 
						|
exit:
 | 
						|
    if(fin){
 | 
						|
        fclose(fin);
 | 
						|
    }
 | 
						|
 | 
						|
    if(fout){
 | 
						|
        fclose(fout);
 | 
						|
    }
 | 
						|
}
 |