gpt4 book ai didi

Checking whether a character in a string is a space does not work(检查字符串中的字符是否为空格不起作用)

转载 作者:bug小助手 更新时间:2023-10-25 12:03:59 27 4
gpt4 key购买 nike



I need to add two zeroes before each part with 6 numbers in a string without deleting the spaces between these parts. Here's the code:

我需要在每个部分之前添加两个零,在不删除这些部分之间的空格的情况下,在字符串中添加6个数字。代码如下:


#include <iostream>
#include <string>
using namespace std;

int main()
{
string EightByte_BinaryBuffer = "010000 010100 001001 000011"; //can provide full code with reading input from user if needed

for (int i = 0; i < EightByte_BinaryBuffer.length(); i++)
{
if (i == 0)
{
EightByte_BinaryBuffer.insert(i, "00");
}
else if (isspace(EightByte_BinaryBuffer.at(i)) == true) //output console just stays black for minute without any changes
{
EightByte_BinaryBuffer.insert(i, "00");
}
}
cout << EightByte_BinaryBuffer << endl;
}

I also tried this alternative, but it didn't work either:

我也试过这个办法,但也不管用:


#include <iostream>
#include <string>
using namespace std;

int main()
{
string EightByte_BinaryBuffer = "010000 010100 001001 000011"; //can provide full code with reading input from user if needed

for (int i = 0; i < EightByte_BinaryBuffer.length(); i++)
{
if (i == 0)
{
EightByte_BinaryBuffer.insert(i, "00");
}
else if (EightByte_BinaryBuffer[i] == ' ')
{
EightByte_BinaryBuffer.insert(i, "00");
}
}
cout << EightByte_BinaryBuffer << endl;
}

For context, I'm trying to replicate a base64 converter, but stuck on step 5 (converting six-bit bytes into eight-bit bytes). Here is the full algorithm: https://base64.guru/learn/base64-algorithm/encode

对于上下文,我尝试复制一个Base64转换器,但停留在第5步(将6位字节转换为8位字节)。以下是完整的算法:https://base64.guru/learn/base64-algorithm/encode


更多回答

I don't understand, why are you inserting two binary digits each time?

我不明白,你为什么每次都要插入两位二进制数?

You're making the string longer without adjusting i. Every time you encounter a space, you're inserting characters at i, pushing the space back into the future. You encounter the first space an infinite number of times.

您在不调整i的情况下将字符串加长。每次遇到空格时,您都会在i处插入字符,将该空格推回到未来。你会无数次地遇到第一个空间。

What does "didn't work" look like? You seem to be saying there's a problem with your code, but you haven't really described what that problem is

“没成功”是什么样子的?您似乎是在说您的代码有问题,但您并没有真正描述问题是什么

Is this your goal?.

这是你的目标吗?

@ThomasMatthews it's part of base64 encoding algorhytm

@ThomasMatthews它是Base64编码算法的一部分

优秀答案推荐

Thanks @brian61354270 for pointing out my mistake with "i" counter,i was able to fix it:

感谢@brian61354270指出我在“i”计数器上的错误,我能够改正它:


#include <iostream>
#include <string>
using namespace std;

int main()
{
for (int i = 0; i < EightByte_BinaryBuffer.length(); i++)
{
if (i == 0)
{
EightByte_BinaryBuffer.insert(i, "00");
i += 2;
}
else if (EightByte_BinaryBuffer[i + 1] == ' ')
{
EightByte_BinaryBuffer.insert(i + 2, "00");
i += 2;
}
}
}

There's also alternative suggested by @paulmckenzie which makes use of input stream:

@paulmckenzie还建议使用输入流:


//explanation of how this code works can be found in comments under the question

#include <iostream>
#include <string>
#include <sstream>

int main()
{
std::string EightByte_BinaryBuffer = "010000 010100 001001 000011";
std::istringstream strm(EightByte_BinaryBuffer);
std::string small_buf;
std::string new_buf;
while (strm >> small_buf)
new_buf.append("00" + small_buf + " ");
new_buf.pop_back();
std::cout << new_buf;
}

Thanks for help everyone.

感谢大家的帮助。


更多回答

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