Thread: Converting hex values from file to integers

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    4

    Converting hex values from file to integers

    Hi All,
    First up, i am going to tell you that i am not a full-time programmer - i design digital circuits. However i have tinkered with cpp for different tasks. I need for a current project to take a file containing ascii characters - hex values that i need to read, line by line and store in an array of integers so that i can post-process them. The file contents look like the following;

    ABCD
    FFDE
    1234

    etc - i want to read these in and convert them to 16-bit integer numbers.

    I have tried the following.

    Code:
    int main(int argc, char **argv) {
      long memVal[256*8];
      int lineNum=0;
    
      FILE *hexFile;
    
      hexFile = fopen("ppmem.hex","r");
    
      if(hexFile != NULL) {
        cout << "ppmem.hex successfully opened for read" << endl;
    
        while(!feof(hexFile)){ 
          fscanf(hexFile, "%x", &memVal[lineNum]);
          //lineNum < 5 ? lineNum +=5 : lineNum += 4;
          cout << memVal[lineNum] << endl;
          lineNum ++;
        }
        cout << "Total number of lines read: " << lineNum << endl;
        return 0;
      }else{
        cout << "Error opening ppmem.hex" << endl;
        return -1;
      }
    }


    I must be missing something somewhere, the output is

    ppmem.hex successfully opened for read
    lineNum 0 -> 0
    lineNum 1 -> 0
    lineNum 2 -> 0
    lineNum 3 -> 0
    lineNum 4 -> 0
    lineNum 5 -> 0
    lineNum 6 -> 0
    ...
    Total number of lines read: 50
    I would also like to have the loop not require me to enter the number of line - but one problem at a time. Thanks for looking - and sorry for my ignorance.

  2. #2
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Your output does not seem to fit your code. Why are you using C functions in C++? Use ifstream instead, and strings. It'll make your life much easier.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    4
    Sorry, posted the wrong code. It is better to use fstream objects rather than fopen()/close()?


    Code:
    int main(int argc, char **argv) {
      //char Command[300] ;
      //int Interactive;
      long memVal[256*8];
      int lineNum=0;
    
      FILE *hexFile;
    
      hexFile = fopen("ppmem.hex","r");
    
      if(hexFile != NULL) {
        cout << "ppmem.hex successfully opened for read" << endl;
    
        while(lineNum < 50){ 
          fscanf(hexFile, "%x", &memVal[lineNum]);
          cout << "lineNum " << lineNum << " -> " << memVal[lineNum] << endl;
          lineNum ++;
        }
        cout << "Total number of lines read: " << lineNum << endl;
        fclose(hexFile);
        return 0;
      }else{
        cout << "Error opening ppmem.hex" << endl;
        return -1;
      }
    }

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    63
    As the previous poster said, this is C++, not C. I would repeat his suggestion of looking at the various streams. Use ifstream to do any reading. Instead of using an array with a static size, I would suggest that you look into using a vector or list or something instead.

    As far as your current code goes, after plugging it in to my compiler (GCC 4.4.0) and saving your little hex file, it works. The output is as follows:

    43981
    65502
    4660

    It also produces the following warning: "format %x expects type 'unsigned int*', but argument 3 has type 'long int*' " This is different from your reported output, so I have a feeling that the code you have posted does not match the code in your function.


    EDIT:

    I see you've posted different code now. Compiling and running still works, only it prints out 50 lines, even though the file is only 3.
    Last edited by Zach_the_Lizard; 09-24-2009 at 05:07 AM.

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    4
    Sorry for the confusion - i did not post the contents of the file - only something similar. I think the problem is that the first line of my file starts with an @ symbol - this is causing the fprintf function to misbehave. Thanks for the response - i will look at recoding with streams if you think this a better approach.

  6. #6
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by Ingersoll View Post
    Sorry for the confusion - i did not post the contents of the file - only something similar. I think the problem is that the first line of my file starts with an @ symbol - this is causing the fprintf function to misbehave. Thanks for the response - i will look at recoding with streams if you think this a better approach.
    It isn't necessarily better in the sense that it's faster or anything in that direction, but you are writing C++, and mixing C functions with C++ is generally not recommended. The string class is very, very useful, and so are string streams, and it would make sense to use them as they will simply make your life easier.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help converting CANoe logfile to EXCEL readable .txt file
    By turbofizzl in forum C Programming
    Replies: 21
    Last Post: 08-16-2009, 08:56 PM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  4. Replies: 6
    Last Post: 11-11-2006, 02:10 PM
  5. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM