[Solved] Invert 7th bit of hex string C++ [duplicate]


#include <stdio.h>

void flipme(char *buf, const char *inBuf)
{
    int x;
    sscanf(inBuf, "%x", &x);
    x ^= 1 << 17;
    sprintf(buf, "%06X", x);
}

int main(void)
{
    char buf[16];

    flipme(buf, "002A05");
    printf("002A05->%s\n", buf);

    flipme(buf, "ABCDEF");
    printf("ABCDEF->%s\n", buf);
}

Output:

002A05->022A05
ABCDEF->A9CDEF

You wrote:

I tried converting hex string to integer via strtol, but that function strip leading zeros.

The strtol function converts it to a number. It doesn’t mean anything to say it strips leading zeroes because numbers don’t have leading zeroes — “6” and “06” are two different ways of writing the same number. If you want leading zeroes when you print it, you can add them then.

6

solved Invert 7th bit of hex string C++ [duplicate]