Solved

converting a vector of unsigned char to unsigned short

Posted on 2014-01-12
6
745 Views
Last Modified: 2014-01-31
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
0
Comment
Question by:LuckyLucks
6 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 39775647
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];
}

Open in new window

0
 
LVL 32

Accepted Solution

by:
phoffric earned 500 total points
ID: 39775671
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.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 39775833
You can just use memcpy to do this.

vector<unsigned char> ucvec;

// add stuff to ucvec
...

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

Open in new window


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.
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 33

Expert Comment

by:sarabande
ID: 39776509
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();

Open in new window


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
0
 

Author Comment

by:LuckyLucks
ID: 39776651
@Sara:

what do 0xa0, 0xa1,0xa2,0xa3,0xa5a4  mean and how do you come up with those hex numbers ? thanks
0
 
LVL 33

Expert Comment

by:sarabande
ID: 39776691
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
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question