2

I read from a file with memory addresses in the form 7fefe05a8,7fefe0590etc

I would like to store them in an pointer/integer/long etc variable and do some operations on them e.g bit masking.

// store "7fefe05a8" from file into pointer variable
lastFourBits = pointer & 0x00000000f;
pizzaEatingGuy
  • 878
  • 3
  • 10
  • 19
  • 2
    Just use [strtoul](http://linux.die.net/man/3/strtoul) to convert the string to an unsigned long. – Paul R Nov 01 '15 at 08:25
  • 2
    Do *not* use a pointer-typed variable to perform the masking on, but a suitable integer variable. – alk Nov 01 '15 at 08:45
  • 1
    If you're not actually going to be dereferencing them (with `*`) then just treat them as numbers. – user253751 Nov 01 '15 at 11:21

1 Answers1

2

Use strtoul() function to perform conversion. str pointer should contain pointer to your string.

char              *endptr, *str;
unsigned long int pointer;

str = ... /* assign pointer to your string */
pointer = strtoul(str, &endptr, 16);

if (endptr == str) { /* no digits were found */
...
}

if (*endptr != '\0')  { /* further characters after number */
...
}
Maciej
  • 9,355
  • 2
  • 15
  • 18