# converting a vector of unsigned char to unsigned short

Hi

How can i convert a vector (in C++) of unsigned char to a vector of unsigned short? (it would take 2 bytes together and stuff them in the same index.

thanks
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
Well, what about  just a 'for' loop'?

``````void vector_uchar_to_ushort(const std::vector<unsigned char>& v1, std::vector<unsigned short>& v2) {

for (size_t sz = 0; sz != v1.lenght(); ++sz)
v2[sz] = (unsigned short) v1[sz];
}
``````
\Commented:
To stuff the two bytes together into one short, here is an expression to add to jkr's code
static_cast<unsigned short>(v1[sz]) << 8 | v1[++sz]

Since you have only half the number of elements for the unsigned short vector, modify the for-condition:
sz != v1.lenght()/2
You also need to deal with the case of having an odd number of chars.

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Senior Software Engineer (Avast)Commented:
You can just use memcpy to do this.

``````vector<unsigned char> ucvec;

...

vector<unsigned short> usvec(ucvec.size()/2);
memcpy(&usvec[0], &ucvec[0], ucvec.size());
``````

Some general caveats to consider...

1. the number of items in ucvec MUST be even
2. sizeof(short) must be equal to sizeof(char)/2
3. the value short represents depends on the endianess of your platform

In short, unless you know what you are doing copying one type to another, in this way, (ie stuffing two chars into a short) will result in non-portable code that, at best, will only work on the platform it is built and tested on. It is both platform and compiler dependent.
Commented:
depending on the stl implementation (and endianess) of your platform you may even able to cast between the vectors (granted that you have an even number of elements in the char vector). the following compiles and runs on my system (vc10, win7):

std::vector<unsigned char>    vuc;
std::vector<unsigned char>  * pvuc = &vuc;
std::vector<unsigned short> * pvus;

`````` pvuc->push_back(0xa0);
pvuc->push_back(0xa1);
pvuc->push_back(0xa2);
pvuc->push_back(0xa3);

pvus = (std::vector<unsigned short> *)pvuc;
unsigned short us1 = (*pvus)[0];
unsigned short us2 = (*pvus)[1];
size_t szvus = pvus->size();
pvus->push_back(0xa5a4);
size_t szvuc = pvuc->size();
``````

the reason why it worked is that the vector has pointer members to the begin, last, and end (which is the address beyond array boundaries) of its internal array. those pointers are valid for both array types as long as there is no 'odd' index is involved. the size and number of elements are not stored in members but were deduced from those pointers and from template type.

Sara
Author Commented:
@Sara:

what do 0xa0, 0xa1,0xa2,0xa3,0xa5a4  mean and how do you come up with those hex numbers ? thanks
Commented:
it doesn't mean anything. they are some samples for unsigned char or unsigned short which easily could be verified in the debugger.

note, an unsigned char is an integer in the range 0 - 255 decimal or 0x00-0xff hexadecimal.

an unsigned short of 0xa5a4 would map to an array of two unsigned char of 0xa4 and 0xa5 if using the same address and the endianess is little-endian.

Sara
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.