gpt4 book ai didi

C++将字符从静态数组复制到动态数组会添加一堆随机元素

转载 作者:行者123 更新时间:2023-11-30 03:54:14 25 4
gpt4 key购买 nike

我有一个静态数组,但是当将值复制到动态数组时,我得到了一堆废话。我需要生成的动态数组正好是 8 个字符

unsigned char cipherText[9]; //Null terminated
cout<<cipherText<<endl; //outputs = F,ÿi~█ó¡

unsigned char* bytes = new unsigned char[8]; //new dynamic array

//Loop copys each element from static to dynamic array.
for(int x = 0; x < 8; x++)
{
bytes[x] = cipherText[x];
}

cout<<bytes; //output: F,ÿi~█ó¡²²²²½½½½½½½½ε■ε■

最佳答案

您需要更新代码以复制空终止符:

unsigned char* bytes = new unsigned char[9];  //new dynamic array

//Loop copys each element from static to dynamic array.
for(int x = 0; x < 9; x++)
{
bytes[x] = cipherText[x];
}

cout<<bytes;

这是假设 cipherText 实际上包含一个以 null 结尾的字符串。

关于C++将字符从静态数组复制到动态数组会添加一堆随机元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29690675/

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