Basically, I have a file which contains multiple lines that I have to parse. For example, I may have something of the form: "Homer Simpson ate @0x000000000000BEEF donuts"
Recovering the name is simple, it's recovering the number of donuts he ate that I have trouble with. In other words, I would like to store this number in a uint64_t variable.
My first approach was to simply read the number from character to character and convert it to a decimal (i.e. I would compute 0*16^15 + 0*16^14 + ... + E *16^1 + F*16^0). This method seems very costly to me and I am sure there must be a better way for me to achieve this. However, this technique had the advantage that if the number was written like this "@0x0000 0000 0000 BEEF", I would still be able to convert it to a uint64_t variable.
The second method I tried was to use sscanf or strol (as described in many other posts, such as this one: Convert hex string (char []) to int?). The problem with this method is that I first have to create a substring "@0x000000000000BEEF" from the original string (same goes for strol). Again, this technique works, but it wouldn't work if I had "@0x0000 0000 0000 BEEF.
Is there a clever and easy way for me to recover this number and store it in a uint64_t variable?
This question is not the same as in other posts. All they have to do it to convert a hex string into a uint64_t. My question is different.
I have a string which is not simply composed of the hex-value, the format of the hex-value may changes from line to line and may have whitespace separated words. For example, I may have:
"Bart annoyed @00000000000000AA people today"
Or the hex value may be written like this:
"Bart annoyed @0000 0000 0000 00AA people today"
How can I read each line a form the hex number as a string that can then be converted as shown in the other answers?