gpt4 book ai didi

c++ - strcpy/追加字符串的问题

转载 作者:行者123 更新时间:2023-11-27 22:50:14 25 4
gpt4 key购买 nike

所以我正在尝试为游戏制作一个名称更改器。代表玩家代号的偏移量是 0x838BA824;我正在尝试制作一个循环,它会像彩虹一样改变名字的颜色。因此,当我启用“彩虹”-gamertag 时,我会这样做:

LastName = (char*)0x838BA824;//char *LastName;
Rainbow = !Rainbow;//a boolean used to toggle

然后在钩环中我这样做:

if(Rainbow)//the toggle boolean
{
printf("LastName: %s\n", LastName);
peep++;//int peep;
if(peep >= 8)
peep = 0;
switch(peep)
{
case 0:
printf("0\n");
Buffer += "^0";//std::string buffer || ^0-9 are the colorcodes for that game, so here it adds the color
Buffer += LastName;//then it adds the name
printf("Buffer: %s\n", Buffer.c_str());
strcpy((char*)0x838BA824, Buffer.c_str());//then it sets the string with the color
Buffer = "";//it clears the Buffer
printf("Buffer: %s\n", Buffer.c_str());
break;
case 1:
printf("1\n");
Buffer += "^1";
Buffer += LastName;
printf("Buffer: %s\n", Buffer.c_str());
strcpy((char*)0x838BA824, Buffer.c_str());
Buffer = "";
printf("Buffer: %s\n", Buffer.c_str());
break;
case 2:
printf("2\n");
Buffer += "^2";
Buffer += LastName;
printf("Buffer: %s\n", Buffer.c_str());
strcpy((char*)0x838BA824, Buffer.c_str());
Buffer = "";
printf("Buffer: %s\n", Buffer.c_str());
break;
case 3:
printf("3\n");
Buffer += "^3";
Buffer += LastName;
printf("Buffer: %s\n", Buffer.c_str());
strcpy((char*)0x838BA824, Buffer.c_str());
Buffer = "";
printf("Buffer: %s\n", Buffer.c_str());
break;
case 4:
printf("4\n");
Buffer += "^4";
Buffer += LastName;
printf("Buffer: %s\n", Buffer.c_str());
strcpy((char*)0x838BA824, Buffer.c_str());
Buffer = "";
printf("Buffer: %s\n", Buffer.c_str());
break;
case 5:
printf("5\n");
Buffer += "^5";
Buffer += LastName;
printf("Buffer: %s\n", Buffer.c_str());
strcpy((char*)0x838BA824, Buffer.c_str());
Buffer = "";
printf("Buffer: %s\n", Buffer.c_str());
break;
case 6:
printf("6\n");
Buffer += "^6";
Buffer += LastName;
printf("Buffer: %s\n", Buffer.c_str());
strcpy((char*)0x838BA824, Buffer.c_str());
Buffer = "";
printf("Buffer: %s\n", Buffer.c_str());
break;
case 7:
printf("7\n");
Buffer += "^7";
Buffer += LastName;
printf("Buffer: %s\n", Buffer.c_str());
strcpy((char*)0x838BA824, Buffer.c_str());
Buffer = "";
printf("Buffer: %s\n", Buffer.c_str());
break;
}
}

编辑:我也可以在钩子(Hook)中调用它(相同的结果-.-)

void changeName()
{
printf("LastName: %s\n", LastName);
peep++;
if(peep >= 8)
peep = 0;
printf("%i\n", peep);
Buffer += va("^%i", peep);
Buffer += LastName;
printf("Buffer: %s\n", Buffer.c_str());
strcpy((char*)0x838BA824, Buffer.c_str());
Buffer = "";
printf("Buffer: %s\n", Buffer.c_str());
}

到目前为止,一切对我来说似乎都很好,所以我对其进行了测试,并在我的控制台中将其作为输出:

LastName: Test
1
Buffer: ^1Test
Buffer:
LastName: ^1Test
2
Buffer: ^2^1Test
Buffer:
LastName: ^2^1Test
3
Buffer: ^3^2^1Test
Buffer:
LastName: ^3^2^1Test
4
Buffer: ^4^3^2^1Test
Buffer:
LastName: ^4^3^2^1Test
5
Buffer: ^5^4^3^2^1Test
Buffer:
LastName: ^5^4^3^2^1Test
6
Buffer: ^6^5^4^3^2^1Test
Buffer:
LastName: ^6^5^4^3^2^1Test
7
Buffer: ^7^6^5^4^3^2^1Test
Buffer:
LastName: ^7^6^5^4^3^2^1Test
0
Buffer: ^0^7^6^5^4^3^2^1Test
Buffer:
LastName: ^0^7^6^5^4^3^2^1Test
1
Buffer: ^1^0^7^6^5^4^3^2^1Test
Buffer:
LastName: ^1^0^7^6^5^4^3^2^1Test
2
Buffer: ^2^1^0^7^6^5^4^3^2^1Test
Buffer:
LastName: ^2^1^0^7^6^5^4^3^2^1Test
3
Buffer: ^3^2^1^0^7^6^5^4^3^2^1Test
Buffer:
LastName: ^3^2^1^0^7^6^5^4^3^2^1Test
4
Buffer: ^4^3^2^1^0^7^6^5^4^3^2^1Test
Buffer:
LastName: ^4^3^2^1^0^7^6^5^4^3^2^1Test
5
Buffer: ^5^4^3^2^1^0^7^6^5^4^3^2^1Test
Buffer:
LastName: ^5^4^3^2^1^0^7^6^5^4^3^2^1Test
6
Buffer: ^6^5^4^3^2^1^0^7^6^5^4^3^2^1Test
Buffer:
LastName: ^6^5^4^3^2^1^0^7^6^5^4^3^2^1Test
7
Buffer: ^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1Test
Buffer:
LastName: ^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1Test
0
Buffer: ^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1Test
Buffer:
LastName: ^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1Test
1
Buffer: ^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1Test
Buffer:
LastName: ^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1Test
2
Buffer: ^2^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1Test
Buffer:
LastName: ^2^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1Test
3
Buffer: ^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1Test
Buffer:
LastName: ^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1Test
4
Buffer: ^4^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1Test
Buffer:
LastName: ^4^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1Test
5
Buffer: ^5^4^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1Test
Buffer:
LastName: ^5^4^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1Test
6
Buffer: ^6^5^4^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1Test
Buffer:
LastName: ^6^5^4^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1Test
7
Buffer: ^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1Test
Buffer:
LastName: ^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1Test
0
Buffer: ^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1Test
Buffer:
LastName: ^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1Test
1
Buffer: ^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1Test
Buffer:
LastName: ^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1Test
2
Buffer: ^2^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1Test
Buffer:
LastName: ^2^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1Test
3
Buffer: ^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1Test
Buffer:
LastName: ^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1Test
4
Buffer: ^4^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1Test
Buffer:
LastName: ^4^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1Test
5
Buffer: ^5^4^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1Test
Buffer:
LastName: ^5^4^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1Test
6
Buffer: ^6^5^4^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1Test
Buffer:
LastName: ^6^5^4^3^2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^4^3^2^1^0^7^6^5^4^3^2^1
Buffer:
LastName: ^4^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5
5
Buffer: ^5^4^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5
Buffer:
LastName: ^5^4^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5
6
Buffer: ^6^5^4^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5
Buffer:
LastName: ^6^5^4^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5
7
Buffer: ^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5^4^3^2^1^0^7^6^5
Buffer:

任何人都知道为什么它没有正确清除缓冲区(或者为什么它也将姓氏设置为缓冲区)或者为什么会发生这种情况?通常输出应该是这样的,如果它正确的话:

LastName: Test
1
Buffer: ^1Test
Buffer:
LastName: Test
2
Buffer: ^2Test
Buffer:
LastName: Test
3
Buffer: ^3Test
Buffer:
LastName: Test
4
Buffer: ^4Test
Buffer:
LastName: Test
5
Buffer: ^5Test
Buffer:
LastName: Test
6
Buffer: ^6Test
Buffer:
LastName: Test
7
Buffer: ^7Test
Buffer:
LastName: Test

最佳答案

LastName 是指向 0x838BA824 的指针,这是您使用 strcpy 写入的地址。

每次更新玩家代号时,您都会重新读取 LastName 中的名称,因此您会取回上次迭代时放入其中的内容。

我可以建议两种方法来纠正此问题:

1) 首次启用时将 std::string 初始化为 LastName,之后不要重新读取 LastName

char *LastNamePtr = (char*)0x838BA824;
std::string LastNameString(LastNamePtr);
Rainbow = true;

// ...

if (Rainbow) {
peep = (peep + 1) & 7;
std::string Buffer = "^" + std::to_string(peep) + LastNameString;
strcpy(LastNamePtr, Buffer.c_str());
}

或 2) 在重新读取字符串时检测到“^”字符,如果是,则更改数字而不是添加“^”和数字

char *LastNamePtr = (char*)0x838BA824;
Rainbow = true;

// ...

if (Rainbow) {
peep = (peep + 1) & 7;
if (LastNamePtr[0] == '^') { // check first character of string
LastNamePtr[1] = peep + '0'; // update second character of string
} else {
std::string Buffer = "^" + std::to_string(peep) + std::string(LastNamePtr);
strcpy(LastNamePtr, Buffer.c_str());
}
}

无论哪种情况,请注意 peep = (peep + 1) & 7 是一种更简洁的书写方式

peep++;
if (peep >= 8)
peep = 0;

并且我已经使用 std::to_stringpeep + '0' 从 int 中获取数字,而无需使用开关。

关于c++ - strcpy/追加字符串的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37999523/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com