Convert hexadecimal to decimal

I am looking for a way to convert hex (hexadecimal) to dec (decimal) easily. I found a simple way to do this, for example:

 int k = 0x265; cout << k << endl; 

But with that I can’t enter 265 . In any case, for it to work like this:

Entrance: 265

Output: 613

Is there any way to do this?

Note: I tried:

 int k = 0x, b; cin >> b; cout << k + b << endl; 

and it does not work.

+6
source share
6 answers
 #include <iostream> #include <iomanip> int main() { int x, y; std::stringstream stream; std::cin >> x; stream << x; stream >> std::hex >> y; std::cout << y; return 0; } 
+14
source

Use the std::hex manipulator:

 #include <iostream> #include <iomanip> int main() { int x; std::cin >> std::hex >> x; std::cout << x << std::endl; return 0; } 
+10
source

Well, method C might be something like ...

 #include <stdlib.h> #include <stdio.h> int main() { int n; scanf("%d", &n); printf("%X", n); exit(0); } 
+5
source

Here is a solution using strings and converting it to decimal with ASCII tables:

 #include <iostream> #include <string> #include "math.h" using namespace std; unsigned long hex2dec(string hex) { unsigned long result = 0; for (int i=0; i<hex.length(); i++) { if (hex[i]>=48 && hex[i]<=57) { result += (hex[i]-48)*pow(16,hex.length()-i-1); } else if (hex[i]>=65 && hex[i]<=70) { result += (hex[i]-55)*pow(16,hex.length( )-i-1); } else if (hex[i]>=97 && hex[i]<=102) { result += (hex[i]-87)*pow(16,hex.length()-i-1); } } return result; } int main(int argc, const char * argv[]) { string hex_str; cin >> hex_str; cout << hex2dec(hex_str) << endl; return 0; } 
+3
source

I use this:

 template <typename T> bool fromHex(const std::string& hexValue, T& result) { std::stringstream ss; ss << std::hex << hexValue; ss >> result; return !ss.fail(); } 
0
source
  std::cout << "Enter decimal number: " ; std::cin >> input ; std::cout << "0x" << std::hex << input << '\n' ; 

if adding an input, which can be boolean or float or int, will be passed back in the internal function call ...

Using function templates based on argument types, C generates separate functions to properly manage each type of call. All definitions of function templates begin with a keyword template, followed by arguments enclosed in angle brackets <and>. For the type of data to be tested, one formal parameter T is used.

Consider the following program, in which the user is prompted to enter an integer, and then a float, each of which uses a square function to determine the square. Using function templates based on argument types, C generates separate functions to properly manage each type of call. All definitions of function templates begin with a keyword template, followed by arguments enclosed in angle brackets <and>. For the type of data to be tested, one formal parameter T is used.

Consider the following program, in which the user is prompted to enter an integer, and then a float, each of which uses a square function to determine the square.

 #include <iostream> using namespace std; template <class T> // function template T square(T); /* returns a value of type T and accepts type T (int or float or whatever) */ void main() { int x, y; float w, z; cout << "Enter a integer: "; cin >> x; y = square(x); cout << "The square of that number is: " << y << endl; cout << "Enter a float: "; cin >> w; z = square(w); cout << "The square of that number is: " << z << endl; } template <class T> // function template T square(T u) //accepts a parameter u of type T (int or float) { return u * u; } Here is the output: Enter a integer: 5 The square of that number is: 25 Enter a float: 5.3 The square of that number is: 28.09 
0
source

Source: https://habr.com/ru/post/918080/


All Articles