Thread: Converting a String to Another String

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    2

    Question Converting a String to Another String

    Hi guys and girls,

    The title is a little off, but I didn't really know what to title it. Anyways, Im writing a virtual CPU program in C, and essentially I'm working on a function that opens an asm file, and then converts the operands to binary and saves it to a bcd file. Issue I'm having is, certain operands in the ASM file, I want to converted to specific binary. So as an example, if the operation NOP is read in the .asm file, convert it to 000000 (6bit opcode fields). The highest operand I have is 0x13h, or 010011b.

    As of right now, Im thinking of using strtok() to break up the asm file, and then compare each line of text to my 20 operands, and then if equal, save as a new string equal to the binary, and then append that to my new bcd file. But that is seeming to be a little tricky and finicky. There has to be a better way to do this. Any help is appreciated!

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    strtok would be a good starting point if all you have is opcodes and no operands. but i would use lex/gnu flex. then all you have to do is specify the grammar, which in your case would be pretty easy. more learning curve up front, easier in the long run to incorporate labels and operands.

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    2
    As this is a school project, I need to keep it universal. If I use flex on my home machine, all would be lost for testing by my Prof, or for use on another machine. Below is my chunk of code where Im opening the file, copying it to a new buffer, tokenizing it, and then comparing each token to a string, then converting it to a constant of the same name with the hexadecimal value of each binary opcode. Only problem right now is it throws a segmentation fault. Obviously right now I have it only checking for NOP, for testing. But Im thinking the segmentation fault is coming from using character pointers as buffers, and using strtok on those pointers? Not entirely sure though.

    Code:
        if((fp2 = fopen(newfile, "w+")) == NULL){
                printf("file Name: %s\n", newfile);
                printf("Trouble creating file..\n");
                perror("error: ");
            }
            else
            {
            while(!feof(fp2))
            {    
                strcpy(newbuffer, buffer);
                tok = strtok(newbuffer, ",.-");
                while(tok !=NULL)
                {
                    while(strcmp(tok, 'NOP') != 0)
                    {
                    tok = NOP;
                    fputs(tok, fp2);
                    tok = strtok(NULL, ",.-");
                    }
                }
                fclose(fp2);
            }
            }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. converting a hex value into a string
    By droseman in forum C Programming
    Replies: 12
    Last Post: 11-12-2009, 12:10 PM
  2. converting string to int!
    By aama100 in forum C++ Programming
    Replies: 4
    Last Post: 03-22-2008, 06:11 AM
  3. converting string to int back to string
    By thenson in forum C Programming
    Replies: 4
    Last Post: 02-03-2008, 11:21 AM
  4. C++: Converting Numeric String to Alpha String
    By JosephCardsFan in forum C++ Programming
    Replies: 3
    Last Post: 02-16-2005, 07:07 AM
  5. converting string to int
    By oomen3 in forum C++ Programming
    Replies: 4
    Last Post: 10-28-2002, 06:13 AM

Tags for this Thread