Let's assume I have:
char str[]="0x7fffffffe181"
basically I want to decrement the hex to 0x7fffffffe180
.
How do I do this?
Note: if the final character is letter e.g c
after decrement it should be b
Let's assume I have:
char str[]="0x7fffffffe181"
basically I want to decrement the hex to 0x7fffffffe180
.
How do I do this?
Note: if the final character is letter e.g c
after decrement it should be b
You can convert it to an integer and back like this:
char *str2;
unsigned long long int len, a = strtoull(str, NULL, 16);
a--;
len = snprintf(NULL, 0, "0x%llx", a);
str2 = malloc(len+1);
sprintf(str2, "0x%llx", a);
It only has to work in the range of an unsigned long long.
const char *eptr;
unsigned long long numeric = strtoull(str, &eptr, 16);
if (eptr == str || numeric == 0) {
/* handle error */
}
sprintf(str, "0x%llx", numeric - 1);
We can't do this for signed hex in place because decrementing 0 or certain negative numbers would make the string longer.
In theory, 0 should work for the third argument to strtoull, but it's buggy in our libc right now so it doesn't.