开发者

Incrementing Individual Characters in String

开发者 https://www.devze.com 2023-03-07 01:58 出处:网络
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 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.


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.


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"?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号