I searched Google and they all convert numbers using remainder. My instructor does not permit to use module operator when converting hexadecimal numbers. Also, the use of while keyword and std::hex are not permitted also.
However, he did give me a hint : Handle each digit from left to right.
0x1FA5CC99
I need to input hexadecimal numbers as string then return the result as an integer.
#include <iostream>
#include <string>
#include <stdexcept>
// meets your instructor's specifications: no %, no while, no std::hex
// throws std::invalid_argument if the string is badly formed
// throws std::out_of_range if the converted value would be out of the the range of unsigned long long
unsignedlonglong hex_to_integer( std::string hex_string )
{
std::size_t pos = 0 ;
unsignedlonglong integer = std::stoull( hex_string, std::addressof(pos), 16 ) ;
if( pos == hex_string.size() ) return integer ;
elsethrow std::invalid_argument( "entire string could not be converted" ) ;
}
int main()
{
std::cout << hex_to_integer( "0x1FA5CC99" ) << '\n' ; // 530959513
}
My Pow function does not work for large exponents. How can I fix it?
I have to use Pow to solve the problem that is why my instructor told me to write the Pow function and do not use pow.
I appreciate other solutions but let me know I am on the right track.