递增字符串中的单个字符

发布于 2024-11-08 16:09:31 字数 381 浏览 0 评论 0原文

我不知道我的标题是否正确,所以如果我错了,请纠正我,我会更改我的标题。

我有一个字符串,在这个例子中我将使用:

"8ce4b16b"

我想将位(我认为)沿着 1 移动,所以字符串将是:

"9df5c27c"

有什么想法吗?

编辑:

正如您所知,这些字符串是十六进制的。所以它永远不会到达z。 我想做的就是在数字中添加一个数字,然后在字母表中前进一步,所以 a->b、f->g 等

如果数字是 9,则有一个条件将其保留为 9

。输出不需要是十六进制。

该字符串也只是一个示例。它是 MD5 加密的一部分。

I dont know if I have the correct tiltle for this, so please correct me if I am wrong and I will change my title.

I have a string, for this example I will use:

"8ce4b16b"

I would like to shift the bits (I think) along 1 so the string would be:

"9df5c27c"

Any Ideas?

EDIT:

Just so you know, these strings are hex. So it will never reach z.
All I want to do is add a number to the numbers and progress one step through the alphabet so a->b, f->g ect ect

If the number is 9 there will be a condition to keep it as 9.

The output DOES NOT need to be a hex.

Also the string is only an example. It is part of an MD5 encryption.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

安静被遗忘 2024-11-15 16:09:31

转换字符串?这听起来像是 std::transform()

#include <cassert>
#include <string>

char increment(char c)
{
    if ('9' == c)
    {
        return '9';
    }
    return ++c;
}

std::string increment_string(const std::string& in)
{
    std::string out;
    std::transform(in.begin(), in.end(), std::back_inserter(out), increment);
    return out;
}

int main()
{
    assert(increment_string("8ce4b16b") == "9df5c27c");
    assert(increment_string("ffffffff") == "gggggggg");
    assert(increment_string("89898989") == "99999999"); // N.B: this is one of 2^8 strings that will return "99999999"
    assert(increment_string("99999999") == "99999999"); // This is one more. Mapping backwards is going to be tricky!
    return 1;
}

您希望对字符施加的任何限制都可以在 increment() 函数中实现,如所示。

另一方面,如果您希望将字符串视为十六进制数字并向其中添加 0x11111111

#include <sstream>
#include <cassert>

int main()
{
    std::istringstream iss("8ce4b16b");
    long int i;
    iss >> std::hex >> i;
    i += 0x11111111;
    std::ostringstream oss;
    oss << std::hex << i;
    assert(oss.str() == "9df5c27c");
    return 1;
}

在此字符串 的构造过程中不会移动任何位。

Transform a string? This sounds like a job for std::transform():

#include <cassert>
#include <string>

char increment(char c)
{
    if ('9' == c)
    {
        return '9';
    }
    return ++c;
}

std::string increment_string(const std::string& in)
{
    std::string out;
    std::transform(in.begin(), in.end(), std::back_inserter(out), increment);
    return out;
}

int main()
{
    assert(increment_string("8ce4b16b") == "9df5c27c");
    assert(increment_string("ffffffff") == "gggggggg");
    assert(increment_string("89898989") == "99999999"); // N.B: this is one of 2^8 strings that will return "99999999"
    assert(increment_string("99999999") == "99999999"); // This is one more. Mapping backwards is going to be tricky!
    return 1;
}

Any limits you wish to impose on the characters can be implemented in the increment() function, as demonstrated.

If, on the other hand, you wish to treat the string as a hexadecimal number and add 0x11111111 to it:

#include <sstream>
#include <cassert>

int main()
{
    std::istringstream iss("8ce4b16b");
    long int i;
    iss >> std::hex >> i;
    i += 0x11111111;
    std::ostringstream oss;
    oss << std::hex << i;
    assert(oss.str() == "9df5c27c");
    return 1;
}

No bits were shifted in the construction of this string.

单挑你×的.吻 2024-11-15 16:09:31

看起来您只是将 0x11111111 添加到整数中。但是你能准确指定你的输入有什么类型吗?当“f”或“9”加一时,结果应该是什么?

It looks like you simply added 0x11111111 to the integer. But can you specify precisely what tpye your input has? And what the result should be when you add one to "f" or "9"?

王权女流氓 2024-11-15 16:09:31

这不是移位...移位一位会将字值乘以 2。您只需将每个十六进制值增加 1,这可以通过将 0x11111111 添加到双字来完成。

例如,如果您将值0x8ce4b16b(这会将上面打印的值视为十六进制的 4 字节双字),将其移动一位,您将最终为0x19C962D6

但是,如果您只是想增加双字的每个半字节(十六进制数字中的每个单独值代表 4 位或一个半字节),则必须向每个半字节添加 0x1 的偏移量。此外,十六进制字中没有 G 值...您的值是 0->9,然后是 A->F< /code>,其中 F 代表以 10 为基数的值 15。最后,当您将 0x1 添加到 0xF 时,您将换行大约0x0

That's not shifting the bits ... shifting a bit multiplies a word value by 2. You're simply incrementing each hex value by 1, and that can be done by adding 0x11111111 to your dword.

For instance, if you took your value 0x8ce4b16b (that would be treating the values you printed above as-if they were a 4-byte double-word in hexadecimal), shifting it by one bit, you would end up with 0x19C962D6.

But if you simply want to increment each nibble of your dword (each individual value in a hex-number represents 4-bits or a nibble), you're going to have to add an offset of 0x1 to each nibble. Also there is no value of G in a hex-word ... you have the values 0->9, and then A->F, where F represents the base-10 value 15. Finally, when you add 0x1 to 0xF, you're going to wrap around to 0x0.

ゃ懵逼小萝莉 2024-11-15 16:09:31

您的意思是要增加字符串中的每个字符吗?
您可以通过迭代数组并向每个字符添加一个来做到这一点。

Do you mean you want to increment each character in the string?
You can do that my iterating through the array and adding one to each character.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文