Please tell me how to convert from hex to decimal number. C ++ language

  • Author's thoughts are not enough ... I can tell you: translate every 16-digit number into a 2-digit one (there will be a block of 4 zeros and ones), then - from binary to our native. I guess it's not difficult ... - 3JIoi_Hy6

4 answers 4

In C++ this is correctly done as follows:

 const std::string hex("0x31c3"); std::istringstream stream(hex); int dec; stream >> std::hex >> dec; 

    If there are no special requirements, for example, an unlimited input string at the input and output, then it is possible to use the strtol function from stdlib.h. Then

     char *input = "dead"; char *next; long int value = strtol(input, &next, 16); 
    • one
      Scanf (sscanf) with type specifier% x is still valid - gecube
    • In general, a strange question, and in what number system does the author of the question usually derive? Perhaps he does not know about the existence of sprintf ()? - avp

    As an option

     //num - Число в 16-ричном формате int Numeral(const char*num){ return static_cast<const int>(strtol(num, NULL, 16)); } 
       #include<iostream> using namespace std; int main(){ int i; cin >> hex >> i; cout << dec << i << endl; return 0; }