typecasting char* to short array

  • Follow


Hi,
    can someone pl. help me to convert character array into short
array.
 actually I need in one function. but currently I have input as
character array.

Thanks


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply rahul.ruikar (25) 5/9/2005 10:21:15 PM

#include <iostream>
#include <vector>
#include <iterator>
#include <cstring>

typedef std::vector<short> ShortVector;

ShortVector convert(const char* charArray) {
   const size_t length = strlen(charArray);
     // the output vector is not "terminated" with
     // a zero as a c-style string is. Add one to
     // above calculation for such an effect.

   ShortVector result(length);
   std::copy(charArray,
             charArray + length,
             result.begin());
   return result;
}

int main() {

   ShortVector shortVector =
     convert("comp.lang.c++.moderated");

   std::copy(shortVector.begin(),
             shortVector.end(),
             std::ostream_iterator<short>(std::cout, " "));
   std::cout << '\n';

   // use &shortVector.front() to pass to a function
   // requiring a short* parameter
}


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Neil 5/20/2005 10:21:08 AM


1 Replies
298 Views

(page loaded in 0.084 seconds)

Similiar Articles:













7/23/2012 5:33:06 AM


Reply: