0

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.

user994572
  • 139
  • 1
  • 11
  • 1
    Why would you need win32? – David Heffernan Jul 17 '15 at 20:54
  • Note: that isn't a character array as stated in the question title; it is a `short` array. And I'm genuinely curious to know the *real* problem you're trying to solve. – WhozCraig Jul 17 '15 at 20:56
  • @DavidHeffernan: Because an old environment requires that. legacy code have to patch it up. Someone gives me a string and I convert character array or short (haven't decided how the input device will take it) and feed that to the input device. – user994572 Jul 17 '15 at 21:02
  • 2
    @WhozCraig I'd say the OP is trying to convert a `const char*` into a `const wchar_t*`. Sometimes VC++ compiler errors are a bit misleading... – rodrigo Jul 17 '15 at 21:03
  • @rodrigo I'd say so too, but it would be ever-so conclusive if the *OP* said so. – WhozCraig Jul 17 '15 at 21:04
  • Please specify whether your project is compiled as Unicode or ASCII (Multibyte). Because LPCTSTR is different for both cases. – i486 Jul 17 '15 at 21:05
  • @i486: compiled as unicode – user994572 Jul 17 '15 at 21:29
  • It looks to me like you don't really understand your problem. In my opinion you need to get that clear first. You don't know how your text is encoded. You don't know how to map it to short. You don't realise that Win32 is the wrong layer for the problem. It would be so much better if you explained your problem, as you see it, and then we could help. – David Heffernan Jul 17 '15 at 21:32
  • @DavidHeffernan: explaining the requirement posted earlier, I have a keyboard layout given in string format(say "00000409" for uslayout). I need to conver that to an array of unsigned short and feed it to LoadKeyboardLayoutW. let me know if you need more clarification. – user994572 Jul 17 '15 at 21:53
  • Sounds to me like you just need to declare the string as Unicode as per http://stackoverflow.com/questions/17753607/initializing-a-lpctstr-lpcwstr – Peter Brittain Jul 17 '15 at 22:44
  • If really building in Unicode mode, then `LPCTSTR` can't point to a `"abc"` string. It would need to be `L"abc"`. Maybe that's just a mistake in the example for this question, but it raises the question of whether the real problem is actually ANSI vs Unicode instead of `wchar_t` vs `short`. – TheUndeadFish Jul 17 '15 at 22:49
  • 3
    @user994572: If you read the [`LoadKeyboardLayout()` documentation](https://msdn.microsoft.com/en-us/library/windows/desktop/ms646305.aspx), it takes an `LPCTSTR` as input. You already have an `LPCTSTR` string, in the proper string format that `LoadKeyboardLayout()` is expecting. **NOTHING NEEDS TO BE CONVERTED**. Simply pass your string value **AS-IS** to `LoadKeyboardLayout()` (not `LoadKeyboardLayoutW()`, unless you change your string to `LP(C)WSTR` to match what `LoadKeyboardLayoutW()` expects). This is what happens when you do not state your *actual requirements* up front. – Remy Lebeau Jul 18 '15 at 00:15
  • 2
    I'm voting to close this question as off-topic because the question asked does not adequately state the real problem needing to be solved. The real problem is a lack of understanding of the Win32 API involved, and the fact that the input data is already in the correct format, so nothing needs to be converted. – Remy Lebeau Jul 18 '15 at 00:20

3 Answers3

1

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?

dufresnb
  • 135
  • 10
  • Sorry I just gave an example "ABC" is of the type LPTSTR. I've corrected the question. – user994572 Jul 17 '15 at 20:53
  • This looks very close to what I want, I can get a character array and then store into a short (atleast for now). But when I use teststr.c_str(). I get left of '.c_str' must have class/struct/union type – user994572 Jul 17 '15 at 21:25
1

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;
}
Chad
  • 18,706
  • 4
  • 46
  • 63
  • 1
    The question is for Win32 and arrays - don't make it complex with C++. – i486 Jul 17 '15 at 21:07
  • The input LPTSTR and not std::string – user994572 Jul 17 '15 at 21:12
  • 1
    Question is tagged `C++` and `std::transform` is the cannonical way to "transform" things. – Chad Jul 17 '15 at 21:12
  • The signedness of `char` is implementation-defined. The expression `static_cast(i)` will yield different results with different compilers. Not only is your proposed solution unnecessarily complex, it's also consistently wrong. – IInspectable Jul 24 '15 at 12:09
0

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).

i486
  • 6,491
  • 4
  • 24
  • 41
  • 1
    Keep in mind that the size of an array and the length of a string contained in that array are two different things. – Keith Thompson Jul 17 '15 at 20:59
  • Yes, I added comments for other case. The question and title are not clear and I am not sure what exactly wants the author. There is Win32 API for ASCII <=> Unicode conversions - that is also possible answer. – i486 Jul 17 '15 at 21:00