Reading bytes, arithmetic operations with them...

Hi there!,
What i want is basically work with the signed value of the bytes of one file (i don't really care about the data inside), in order to make arithmetic operations with that values. To open a file, i'm using the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  ... 
  char* dfm=0;
  vector<double> twv(1,0); 

  ifstream file (datafile[i], ios::in|ios::binary|ios::ate);
  if (file.is_open())
  {
    size = file.tellg();	// knowing the size of the file
    dfm = new char [fs];	// changing the size in the buffer
    file.seekg (0, ios::beg);	// repositioning pointer in the begining
    file.read (dfm, size);	// reading the file, loading to "x"
    file.close();		// closing file

    twv.resize(size,0);
    for (j=0; j<size; j++) {twv[j]=dfm[j];}
    delete [] dfm;
    ...


This works fine, but the data is in char format (bytes), and i need the values in double format in order to handle operations like, for instance:
1
2
3
4
5
6
7
8
...
    double a;
    a=-0.5;
    for (j=1; j<size-2; j+=2) {
      twv[j]+=a*(twv[j-1]+twv[j+1]);
    } 
    twv[fs-1]+=2*a*twv[fs-2];
...


By the way, i have the program working fine using an array with fixed values, so the algorithm is fine.

The problem is when reading the values from a file: no errors compiling, the program "works", but the results do not match with the expected.

Can anyone please point me in the right direction in order to accomplish this?

Any comments/suggestions are welcome.
Last edited on
You're using somewhat generic terms, I'm wondering if you can reword your request another way? I don't understand what data you want to work with if not the data inside of the file.
Thanks for your answer.
I'm sorry if i didn't explain myself well.

I'm opening the file in binary:
 
ifstream file (datafile[i], ios::in|ios::binary|ios::ate);


And when you do that, all that you see are bytes. For instance, when you open an image file (or text one, it doesn't matter) using a hex editor, usually you can see in the right side the ASCII characters, and in the left side the hex values. What i want is to use the double signed values of this hex numbers.

Let's say you read from a file the following hex values: FF, D8, FF, E0, 00. Its corresponding "signed integer" values are: -1, -40, -1, -32, 0.

I want to use that values, but in "signed double" format for arithmetic operations, but when i read from a file i have it the all file in memory but in char format, and this is the problem that i'm having.

I hope this time my explanation is clearer than before.

Really appreciate your comments and suggestions.
Last edited on
Hi there!,
I solved what i want it to do using this small routine:
1
2
3
4
5
6
7
8
    // Converting from char to double
    int datavalue, filesize;
    char* datainmemory;
    double* dataindouble=new double[filesize];

    for (i=0; i<filesize; i++) {
      datavalue=datainmemory[i];
      dataindoubles[i]=double(datavalue);

I think the name of the variables speak for themselves,

Hope this could be helpful for someone.
Maybe there is a better way to do it, so if anyone know a better one, please post it.
Last edited on
Topic archived. No new replies allowed.