how would i write a function that parses a hex number as a string into a decimal integer?
the following header should be:
int parseHex(const string &hexString)

how would i write a function that parses a hex number as a string into a decimal integer?
the following header should be:
int parseHex(const string &hexString)

You could use an atoi(...) type function to convert it to an integer, then you can output it in whatever number base you want to.

Or, if you want to do it manually, divide out and sum the multiples of powers of 16.

i.e. 0x539 = 5*(16^2) + (3*16^1) + 9*(16^0) = 5*256 + 3*16 + 9*1 = 1280 + 48 + 9 = 1337.

The rightmost (least significant bit, LSB) represents 16^0, or 1, times the value 0-F, and so on, with each position to the left increasing the power of 16. So, from right to left, it's some number 0-15 * n where n is increasing from 0 at the right most.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.