简体   繁体   English

C++:转换 Win32 文本框 char -> int -> char 并放回另一个文本框

[英]C++: Convert Win32 textbox char -> int -> char and put back in another textbox

I want to accept text input using a text box, then change the characters to integers and do fun math with my integers, and then put them back into a char array to be printed in another text box.我想使用文本框接受文本输入,然后将字符更改为整数并用我的整数进行有趣的数学运算,然后将它们放回字符数组中以在另一个文本框中打印。

Here is my code:这是我的代码:

int len = GetWindowTextLength(textbox) + 1;
char* text = new char[len];
GetWindowText(textbox, &text[0], len);

int x = 0;
int INTmessage[len];
int ENClen = (len * 2);
char ENCmessage[ENClen];

while (x < len) {
    INTmessage[x] = int(text[x]) - 32;
    x++;
}

int z = 0;
int y = 0;

while (z < ENClen) {
    ENCmessage[z] = (INTmessage[y] % 9);
    ENCmessage[z + 1] = (INTmessage[y] % 10);

    z += 2;
    y++;
}

SetWindowText(textreturn, "");
SetWindowText(textreturn, ENCmessage[0]);

The last line displays a compiler error:最后一行显示编译器错误:

invalid conversion from 'char' to LPCSTR.从“char”到 LPCSTR 的无效转换。

Please specify What you mean by 'I don't know why this does not work'.请具体说明“我不知道为什么这不起作用”是什么意思。 One Error in your code is this:您的代码中的一个错误是:

//This line is incorrect because it converts an address to integer, which has no relation to value of textbox, making decryption impossible.
INTmessage[x] = int(&text[x]) - 32;
//Maybe you may want to use this code: 
INTmessage[x] = int(text[x]) - 32;

The last line fails because you are passing a single char to SetWindowText() (accessing ENCmessage[0] returns the first char in the ENCmessage array).最后一行失败,因为您将单个char传递给SetWindowText() (访问ENCmessage[0]返回ENCmessage数组中的第一个char )。 SetWindowText() expects a char* pointer to a null-terminated string instead. SetWindowText()需要一个指向以空char*结尾的字符串的char*指针。 You can drop the [0] :您可以删除[0]

SetWindowText(textreturn, ENCmessage);

Just make sure that ENCmessage contains a null character after your digit characters.只需确保ENCmessage在您的数字字符之后包含一个空字符。

That being said, your code can be re-written to something more like this:话虽如此,您的代码可以重写为更像这样的内容:

int len = GetWindowTextLength(textbox) + 1;
char* text = new char[len];
len = GetWindowText(textbox, text, len);

int *INTmessage = new int[len];
for(int x = 0; x < len; ++x) {
    INTmessage[x] = int(text[x]) - 32;
}

int ENClen = (len * 2) + 1;
char *ENCmessage = new char[ENClen];

for(int x = 0, y = 0; x < len; ++x, y += 2) {
    ENCmessage[y] = (INTmessage[x] % 9);
    ENCmessage[y + 1] = (INTmessage[x] % 10);
}
ENCmessage[ENCLen-1] = '\0';

SetWindowText(textreturn, ENCmessage);

delete[] INTmessage;
delete[] ENCmessage;
delete[] text;

Or, since you tagged the question as C++, like this instead:或者,因为您将问题标记为 C++,所以改为:

#include <string>
#include <vector>

int len = GetWindowTextLength(textbox) + 1;
std::string text;
text.resize(len);
len = GetWindowText(textbox, &text[0], len);

std::vector<int> INTmessage(len);

for(int x = 0; x < len; ++x) {
    INTmessage[x] = int(text[x]) - 32;
}

int ENClen = (len * 2);
std::string ENCmessage;
ENCmessage.resize(ENClen);

for (int x = 0; y = 0; x < len; ++x, y += 2) {
    ENCmessage[y] = (INTmessage[x] % 9);
    ENCmessage[y + 1] = (INTmessage[x] % 10);
}

SetWindowText(textreturn, ENCmessage.c_str());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM