Thread: Converting binary data in a txt file into HEX

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    9

    Converting binary data in a txt file into HEX

    ]Hi I'm not the best at C but I'm trying to write a C function that basically opens a text file with assembler language does a syntax error check on it and then converts the binary data into hex.

    This is my code so far:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    int main(void) 
    {
        FILE*fname;
        char prompt;
        char filename[15];
        char text[100];
    
    
        printf( "Please enter the name of the file you wish to open: \n" );
    
    
        fgets(filename, sizeof(filename), stdin);
        filename[strlen(filename)-1]='\0';
    
    
        fname = fopen(filename, "r+");
        if (fname == NULL)
        {
            printf( "ERROR the file you have chosen cannot be opened \n" );
            printf( "Please make sure the file exists \n");
            return;
        }
        while (fgets(text,100,fname) != NULL)
            printf("\n%s", text);
        scanf("\n%c",&prompt);
    }

  2. #2
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    OK, what are you having trouble with?
    Code:
    while(!asleep) {
       sheep++;
    }

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    9
    basically I'm not sure on how to read the content of the .txt file and do an error check on it. So the program reads the assembly language in the .txt file and picks up any errors.... for example ld $600 (meant to be ldaa $600)

    also I need help with converting the .txt into .s19 format (hex).

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Why don't you just use an assembler? That is it's purpose after all.

    Jim

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    9
    That's what I'm trying to do, build an assembler. I'm struggling on the conversion part of the txt file.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    If you don't know how to retrieve data from a text file I doubt you are even close to being ready to try to code an assembler.

    Jim

  7. #7
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    Instead of updating the file so that it reads parts of the file and modifies them, you ought to make a copy of the file with the changes you want. You can just read the file from stdin (many shells support pipes and redirects; they're pretty cool in that they help you write programs without annoying prompts) and write the new data to stdout. Easy, and you don't even need to touch fopen or fclose.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. converting a binary file to a text array
    By ckeays in forum C Programming
    Replies: 2
    Last Post: 03-29-2011, 03:39 PM
  2. converting a string into binary for data transfer
    By Bopamo in forum C++ Programming
    Replies: 2
    Last Post: 10-15-2010, 01:39 PM
  3. Converting text file to binary
    By rocketman03 in forum C++ Programming
    Replies: 6
    Last Post: 10-11-2009, 12:43 PM
  4. Replies: 2
    Last Post: 03-03-2004, 07:25 PM
  5. Converting File Data Into Double
    By retretret in forum C++ Programming
    Replies: 1
    Last Post: 03-03-2002, 08:04 AM

Tags for this Thread