c++ - How to copy an integer from vector<char> -


i working on wav files, tring parse them. don't wanna use libraries . open wav file fstream , read data vector. want parse wav file header . example want filesize between 4th , 8th bytes. want assign integer . accomplish memcpy .but since c++ don't wanna use memcpy. solution end :

std::vector<unsigned char>::iterator vectorite = soundfiledatavec.begin(); vawparams.totalfilesize = 0; //since little endian used reverse_copy std::reverse_copy(vectorite + 4, vectorite + 7, (unsigned char*)&vawparams.totalfilesize); 

i not happy (unsigned char*) cast integer pointer .i suspect there better way do. can please advice me better way?

vectors use contiguous storage locations elements, elements can accessed using offsets on regular pointers elements. if don't want memcpy trivial solution is:

int header = *reinterpret_cast<const int*>(&soundfiledatavec[4]); 

this reads 4 bytes (that may need convert 1 endianness another).


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -