You don't need Win32
if you're using C++
.
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
std::string in = "Input";
std::vector<short> out(in.size());
std::transform(
in.begin(),
in.end(),
out.begin(),
[] (const char i) -> short
{
return static_cast<short>(i);
});
for(auto o : out)
std::cout << o << ' ';
std::cout << std::endl;
return 0;
}
Will output: 73 110 112 117 116
int main()
{
char in[] = "Input";
short out[5];
// If you don't know the length,
// then you'll have to call strlen()
// to get a proper begin/end iterator
std::transform(
std::begin(in),
std::end(in),
std::begin(out),
[] (const char i) -> short
{
return static_cast<short>(i);
});
for(auto o : out)
std::cout << o << ' ';
return 0;
}
Realizing that if you don't know the length of the string and have to call strlen()
then you increase the complexity of the problem, here's a more terse function that meets this specific use case.
#include <algorithm>
#include <iterator>
using namespace std;
std::vector<short> to_short_array(char* s)
{
std::vector<short> r;
while(*s != '\0')
{
r.push_back(static_cast<short>(*s));
++s;
}
return r;
}
int main()
{
char stuff[] = "afsdjkl;a rqleifo ;das ";
auto arr = to_short_array(stuff);
for(auto a : arr)
{
std::cout << a << ' ';
}
std::cout << endl;
}