M
mathieu
Hello,
I would like to implement a 'vector<uint12_t>' structure, where
uint12_t is a 12bits unsigned integer. AFAIK I need to completely
duplicate the implementation of let say vector<bool> and adapt the
code to suit my need. I cannot find out if there is way for me to
reuse my vendor std::vector ?
Suggestions welcome,
Mathieu
#include <iostream>
int main(int, char *[])
{
// Let's pretend for a moment those are 6 uint12_t values:
// 0x012 0x345 0x678 0x9ab
const unsigned char in[] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab };
// store them on 16bits:
// 0x0012 0x0345 0x0678 0x09ab
const unsigned int Nin = sizeof(in); // sizeof(unsigned char) == 1
const unsigned int Nout = Nin * 2 / 3;
unsigned short out[Nout];
const unsigned char *p_in = in;
for(unsigned short *p_out = out; p_out != out + Nout; /*p_out+=2*/)
{
const unsigned char b0 = *p_in++;
const unsigned char b1 = *p_in++;
const unsigned char b2 = *p_in++;
*p_out++ = ((b0 >> 4) << 8) + ((b0 & 0x0f) << 4) + (b1 >> 4) ;
*p_out++ = ((b1 & 0x0f) << 8) + ((b2 >> 4) << 4) + (b2 & 0x0f);
}
for(unsigned int i=0; i < Nout; ++i)
std::cout << std::hex << out << " ";
std::cout << std::endl;
return 0;
}
I would like to implement a 'vector<uint12_t>' structure, where
uint12_t is a 12bits unsigned integer. AFAIK I need to completely
duplicate the implementation of let say vector<bool> and adapt the
code to suit my need. I cannot find out if there is way for me to
reuse my vendor std::vector ?
Suggestions welcome,
Mathieu
#include <iostream>
int main(int, char *[])
{
// Let's pretend for a moment those are 6 uint12_t values:
// 0x012 0x345 0x678 0x9ab
const unsigned char in[] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab };
// store them on 16bits:
// 0x0012 0x0345 0x0678 0x09ab
const unsigned int Nin = sizeof(in); // sizeof(unsigned char) == 1
const unsigned int Nout = Nin * 2 / 3;
unsigned short out[Nout];
const unsigned char *p_in = in;
for(unsigned short *p_out = out; p_out != out + Nout; /*p_out+=2*/)
{
const unsigned char b0 = *p_in++;
const unsigned char b1 = *p_in++;
const unsigned char b2 = *p_in++;
*p_out++ = ((b0 >> 4) << 8) + ((b0 & 0x0f) << 4) + (b1 >> 4) ;
*p_out++ = ((b1 & 0x0f) << 8) + ((b2 >> 4) << 4) + (b2 & 0x0f);
}
for(unsigned int i=0; i < Nout; ++i)
std::cout << std::hex << out << " ";
std::cout << std::endl;
return 0;
}