0

I have very little experience with C programming, and in the process of creating a small utility that need to be in C (due to limitation of available compiler on the target) that would read text file containing HEX BYTE ASCII string (sample below, usual files are way too long) and save the output binary file containing the byte characters.

Sample input TEXT file: *The Text file line length is always 32 chars long, except the last line which is less than 32 chars but a divisible of 2.

717585003C6A3578D1EE2FF5FE850654
D234E41D350F5CCDDCCD7F0D15131FDF
C56EFE36A16D0E1AAE520CE531D8B173
EF

Sample Output binary file:

qu….<j5…Ñî/õþ….T.Ò4DÝ5õ\ÍÜÍ.....ß.Ånþ6.m..®R.å1.±s.ï

Sample output as seen in HEX editor; the differences i get in bold

71758500 3C6A3585 D1EE2FF5 FE850654 D23444DD 35F55CCD DCCD7F0D 15131FDF C56EFE36 166D0E1A AE520CE5 3181B173 EF

This is my work so far;

#include <stdio.h>

#define MAXLINE 100

int main() {

    FILE *fr, *fw;
    char str[MAXLINE];
    char* iFile = "A0.txt";
    char* oFile = "APU.bin";
    int i, iNum;
    char b2[2];

    fr = fopen(iFile, "r");
    fw = fopen(oFile, "wb");

    while (fgets(str, MAXLINE, fr) != NULL)
        {
            printf("\n%s", str);
            for(i=0; i< strlen(str);i+=2)
                {
                if (str[i] == '\n')
                    break;
                b2[0] = str[i];
                b2[1] = str[i+1];
                iNum = (int) strtol(b2, NULL, 16);
                printf("\n%s  %c ", b2,iNum);
                fputc((char) iNum,fw);
                }
        }
    fclose(fr);
    fclose(fw);

    return 0;
}

As seen, the output is not 100% accurate to the input, some bytes dont get translated for some reason which i am yet to figure out. help.

***Final code:


#include <stdio.h>

#define MAXLINE 100

int main(int argc, char *argv[]) {
    FILE *fr, *fw;
    char str[MAXLINE];
    char *iFile, *oFile;
    int i, iNum;
    char b2[3];
    b2[2] = '\0';

    if (argc != 3)
        goto err1;

    iFile = argv[1];
    oFile = argv[2];

    if ((fr = fopen( iFile, "r")) == NULL)
            goto err1;

    if ((fw = fopen( oFile, "wb")) == NULL)
            goto err1;
    
    while (fgets(str, MAXLINE, fr) != NULL)
        {
            for(i=0; i< strlen(str);i+=2)
                {
                if (str[i] == '\n')
                    break;
                b2[0] = str[i];
                b2[1] = str[i+1];
                iNum = (int) strtol(b2, NULL, 16);
                fputc((int) iNum,fw);
                }
        }
    fclose(fr);
    fclose(fw);

    return 0;
    
    err1:
    printf("\nError Occured");
    printf("\n\nUsage: prog textin outbin\n");
    return -1;
}

1 Answer 1

2

Nice code! Anyway:

    char b2[2];  
    ...
            b2[0] = str[i];
            b2[1] = str[i+1];
            iNum = (int) strtol(b2, ...)

strtol needs a zero terminated string. b2 is not zero terminated, code is just invalid. Do:

    char b2[3];
    b2[2] = '\0';
    ...
            b2[0] = str[i];
            b2[1] = str[i+1];
            // b2[2] always be '\0'
            iNum = (int) strtol(b2, ...); // ok

Also you may pick a better variable name then b2, like tempstring. Writing descriptive code will help you write less bugs.

fputc((char) iNum,fw);

Don't cast - fputc outputs an unsigned char cast as an int. Just

fputc(iNum, fw);

You code misses a log of error checking - fopen errors, strtol errors and handling lines with an even count of characters.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much for highlighting these. I have incorporated the changes suggested also eliminated the unwanted steps, I had used for error checking and my final output is posted

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.