user994572
user994572

Reputation: 139

Is there a win32 API to convert a string to character array

something like a stringtoshortArray that does this:

short arr[5];
LPCTSTR teststr = "ABC"; // passed as an argument, can be anything
input= stringtoshortarray(teststr, arr);

output:
    arr[0] = 41;
    arr[1] = 42;
    arr[2] = 43;
    arr[3] = 0;

and possibly a function that provides the sizeof the array say

int sz = SizeofArray(arr);
output: 
    sz = 3

could probably code it, but if a library call is there I would like to use it.

Upvotes: 0

Views: 1255

Answers (3)

Chad
Chad

Reputation: 19052

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;
}

Upvotes: 1

i486
i486

Reputation: 6572

Maybe you are asking about lstrlen function - to get length of string? lstrlen works for both ANSI (ASCII) and Unicode strings. If you want to get array static size (no matter from string inside) you can do this:

int len = sizeof arr / sizeof *arr;

It will return the number of elements of array of any element size.

See also MultiByteToWideChar API - I suppose you are looking for it. It will convert char string (array) to Unicode string (short array).

Upvotes: 0

dufresnb
dufresnb

Reputation: 135

http://www.cplusplus.com/reference/string/string/c_str/

Returns a pointer to an array that contains a null-terminated sequence of characters (i.e., a C-string) representing the current value of the string object.

And doesn't the array have to be initialized with some constant? Why do you need to then find out the size?

Upvotes: 1

Related Questions