-2

I need a little bit of help with a hexadecimal calculator I'm trying to make. Hexadecimal calculations require to be able to read both the alphabet and the numbers. So how do I read and assign numerical values to the alphabets while still being able to read numbers? I need to be able to read values 0-9 and assign alphabets A-F with values of 10-15.

3 Answers3

3

You can input a hex value using std::hex.

For example:

int my_value;
std::cin >> std::hex >> my_value;

It also works with std::cout:

std::cout << std::hex << my_value;

Note that my_value is still treated as decimal by default.

Source: Getting hex through Cin


Or you could do this:

std::string input;
std::cin >> input;
int hex = 0;
for(unsigned int i = 0; i < input.length; ++i)
{
    int temp = 0;
    if(input[i] >= '0' && input[i] <= '9')
        temp = (input[i] - '0');
    else if(input[i] >= 'a' && input[i] <= 'f')
        temp = (input[i] - 'a');
    else if(input[i] >= 'A' && input[i] <= 'F')
        temp = (input[i] - 'A');
    temp += i * 16;

    hex =+ temp;
}

A couple of notes:

  • This uses ASCII char arithmetic. It's a bit unreadable.
  • This converts char to int, might cause problems on some systems
  • There's a bug if you add 0s to the left
  • I didn't run this. Probably more bugs.
Community
  • 1
  • 1
Ivan Rubinson
  • 3,001
  • 4
  • 19
  • 48
  • 3
    _"Note that `my_value` stores the value as decimal."_ I doubt that. It will be stored as a binary representation rather. – πάντα ῥεῖ Dec 10 '16 at 10:05
  • Okay, thanks a lot. I used the following code and it seems to be working. #include #include using namespace std; int main () { int xvar; cout <<"Please enter the number in hexadecimal or denary." << endl; std::cin >> std::hex >> xvar; std::cout << std::dec << xvar << endl; std::cout << std::hex << xvar << endl; return 0; } – Pudge Superior Dec 11 '16 at 06:18
  • and here I thought I had to use if else statements. by the way, if I ever had to use something like that elsewhere, how would I input char and int using the same variable? I understood the hexadecimal application, but elsewhere... – Pudge Superior Dec 11 '16 at 06:22
0

you can use string

string s = "cb10"

about how to use, you can just

s[0] = 'a' //s = "ab10"
s[1] = '3' //s = "a310"
int i = s[1] - '0' //change char to int, i = 3
Jiahao Cai
  • 1,222
  • 1
  • 11
  • 25
0

Alright I've been using this piece of code for a while now and it has been working perfectly. thanks all.

#include <iostream>
#include <string>

using namespace std;
int main ()
{
int conv;

cout <<"Please enter appropriate command for conversion;" << endl;
cout <<"1. Hexadecimal to Denary." << endl;
cout <<"2. Denary to Hexadecimal." << endl;

cin >> conv;

if (conv == 1)
{
    int xvar;

    cout <<"Please enter the number in hexadecimal." << endl;

    std::cin >> std::hex >> xvar;

    std::cout << std::hex << xvar << endl;
    std::cout << std::dec << xvar << endl;
}

if (conv == 2)
{
    int yvar;

    cout <<"Please enter the number in denary." << endl;

    std::cin >> std::dec >> yvar;

    std::cout << std::dec << yvar << endl;
    std::cout << std::hex << yvar << endl;

}   
}
Ivan Rubinson
  • 3,001
  • 4
  • 19
  • 48