Phorce
Phorce

Reputation: 2664

Re-casting array to store inside a vector

I'm having a weird problem. I have an array which I need to return to another function. The problem is that it has to be outputted/converted as a

(unsigned)(unsigned char)

The function which handles the data is a char* type. Here is the function in which I am converting the data:

char* Wav::readHeader(ifstream &file)
{
file.read(this->chunkId,                                 4);
file.read(reinterpret_cast<char*>(&this->chunkSize),     4);
file.read(this->format,                                  4);

file.read(this->formatId,                                4);
file.read(reinterpret_cast<char*>(&this->formatSize),    4);
file.read(reinterpret_cast<char*>(&this->format2),       2);
file.read(reinterpret_cast<char*>(&this->numChannels),   2);
file.read(reinterpret_cast<char*>(&this->sampleRate),    4);
file.read(reinterpret_cast<char*>(&this->byteRate),      4);
file.read(reinterpret_cast<char*>(&this->align),         2);
file.read(reinterpret_cast<char*>(&this->bitsPerSample), 4);

char testing[4] = {0};
int testingSize = 0;

while(file.read(testing, 4) && (testing[0] != 'd' ||
                                testing[1] != 'a' ||
                                testing[2] != 't' ||
                                testing[3] != 'a'))
{

    file.read(reinterpret_cast<char*>(&testingSize), 4);
    file.seekg(testingSize, std::ios_base::cur);

}

this->dataId[0] = testing[0];
this->dataId[1] = testing[1];
this->dataId[2] = testing[2];
this->dataId[3] = testing[3];



file.read(reinterpret_cast<char*>(&this->dataSize),     4);

this->data = new char[this->dataSize];

file.read(data,                           this->dataSize);

for(unsigned i=0; (i < 20); i++)
{
    //cout << (unsigned)(unsigned char)data[i] << endl; 

}

return (unsigned)(unsigned char) data;
}

And in another function, I have this:

unsigned char* data = this->readHeader(file);

Can anyone see where I'm going wrong?

Thank you :)

Upvotes: 0

Views: 81

Answers (1)

Pete Becker
Pete Becker

Reputation: 76245

this->data = new char[this->dataSize];

So, presumably, this->data has type char*. Fine: that's the return type for the function, too. So return data seems like the correct code.

Upvotes: 1

Related Questions