[英]Is there a win32 API to convert a string to character array
something like a stringtoshortArray that does this: 像stringtoshortArray这样的东西:
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. 可能会编码,但是如果有库调用,我想使用它。
http://www.cplusplus.com/reference/string/string/c_str/ http://www.cplusplus.com/reference/string/string/c_str/
Returns a pointer to an array that contains a null-terminated sequence of characters (ie, a C-string) representing the current value of the string object. 返回一个指向数组的指针,该数组包含一个以空值终止的字符序列(即C字符串),代表字符串对象的当前值。
And doesn't the array have to be initialized with some constant? 数组是否不必使用某个常量进行初始化? Why do you need to then find out the size? 为什么您需要找出尺寸?
You don't need Win32
if you're using C++
. 如果您使用的是C++
则不需要Win32
。
#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
将输出: 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. 意识到如果您不知道字符串的长度而必须调用strlen()
则会增加问题的复杂性,这是一个更简洁的函数,可以满足此特定用例。
#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;
}
Maybe you are asking about lstrlen
function - to get length of string? 也许您在问lstrlen
函数-获取字符串的长度? lstrlen
works for both ANSI (ASCII) and Unicode strings. lstrlen
适用于ANSI(ASCII)和Unicode字符串。 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. 另请参见MultiByteToWideChar
API-我想您正在寻找它。 It will convert char
string (array) to Unicode string (short array). 它将char
字符串(数组)转换为Unicode字符串(短数组)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.