I've been scouring the internet and my textbooks and haven't found any good explanations about how to incorporate hexadecimal numbers into my C++ code. I am currently working on a program that acts as a simulated computer environment to run a simulated machine language in the console a maximum of 16 instructions long. I already have the code required to read the instruction set from a .txt file and am working on loading it into an array representing memory. I know that machine language instructions are hexadecimal instructions 4 bytes long. My problem is trying to find a way to get my program to read hexadecimal digits as it would any integer or char.

I was debating on whether or not to make a class where I could assign integer values to A, B, C, D, E, and F. But I'm not sure where to go from there. Any help to move me in the right direction is greatly appreciated.

If all you want is to read hexadecimal values like you would decimal values, the std::hex modifier works for both input and output:

#include <iostream>

int main()
{
    int value;

    while (std::cin>> std::hex >> value)
        std::cout<< std::hex << value <<'\t'<< std::dec << value <<'\n';
}

So is hexadecimal already a pre-programmed data type? Like I can just use the insertion and extraction operations to modify the base of the number?

If I were to use the ios_base does that affect the entire program or just a specific block of code? And if I were to change the basefield, would I be able to continue using arrays like I would if I were in decimal form?

Perhaps if I shared my code and expounded on what I am trying to accomplish it would be easier to help.

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

typedef enum  { LOAD='l', RUN='r', CLEAR='c', DUMP='d', QUIT='q', MEMORY='m', REGISTERS='g' } optionType;

int  run ();
int  load();
void dump(char gpr[], char progCount, int instReg);
void clearMemory();
void clearRegisters(char gpr[], char& progCount, int& instReg);
void clearAll();

optionType menu();

int main(int argc, char *argv[])
{
    bool done = false; //Menu control
	char gpr[16]; //General Purpose Registers 0 through F
	char progCount = '0'; //Program Counter
	int instReg = 0; //Instruction Register
    
	while (! done)
    {
          switch(menu())
          {
              case LOAD:  if (load()!= 0) done = true;    break;
              case RUN:   if (run() != 0) done = true;    break;
              case DUMP:  dump(gpr[16]/*I am having problems with this section getting the argument to work correctly*/, progCount, instReg);break;
              case CLEAR: clearAll();                     break;
              case MEMORY: clearMemory();                 break;
              case REGISTERS: clearRegisters(gpr[]/*same here. it says it's expecting an expression*/, progCount, instReg);           break;
              case QUIT:  done = true;
          }
    }
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

int run()
{
    cout << "Run program from address specified by user\n"; //specified by PC
    return 0;
}

int load()
{
	char filename[50]; //stores filename
	ifstream fin; //used for file input

	//Prompt user for file name
	cout << "Enter file name: ";
	cin.getline(filename, 50);
	
	fin.open(filename);

	if(!fin.is_open())
	{ //if file can't open exit program
		exit(EXIT_FAILURE);
	}

	char word[50];
	fin >> word;
	while(fin.good())
	{ //echo back the values read in 2 digit hex form for each number, 2 per line
		cout << word << endl;
		fin >> word;
	}

    // Load program from file specified by user, into Memory...
    return 0;
}

void dump(char gpr[], char progCount, int instReg)
{
	int i = 0; int j = 0; //for use in loop counts
	char loopCountArray[] = {'A', 'B', 'C', 'D', 'E', 'F'}; //my attempt at hex?
	cout << "Registers: \n";
	for(i = 0; i < 10; i++)
		cout << "R" << i << ": " << gpr[i] << endl;
	for(j = 0; j < 6; j++)
	{
		i = 10; //10(dec) = A(hex)
		cout << "R" << loopCountArray[j] << ": " << gpr[i] << endl;
		i++; //increment to continue to B, C...F
	}
	cout << "PC: " << progCount << "  ";
	cout << "IR: " << instReg << endl;
	
    // Display contents of memory and contents of all registers
}

void clearAll()
{
     cout << "Set contents of all memory locations and all registers to 0\n";
     //I figured just running clearMemory() and clearRegisters() would work
}

void clearMemory()
{
     cout << "Set contents of all memory locations to 0\n";
     //Have to set all the data in an array of memory to 0
}

void clearRegisters(char gpr[], char& progCount, int& instReg)
{ //I know I have a lot of work here too
	progCount = '0'; //Program Counter
	instReg = 0; //Instruction Register

	for(int i = 0; i < 16; i++)
		gpr[i] = 0; //sets all contents of gpr array to 0
    // Set contents of all registers to 0
}

optionType menu()
{
    bool ok=false;
    unsigned char option;
    while (!ok)
    {
          cout << "Enter one of the following options:\n";
          cout << char(CLEAR)    << ") to clear memory and registers\n";
          cout << char(MEMORY)   << ") to clear memory only\n";
          cout << char(REGISTERS)<< ") to clear registers only\n";
          cout << char(LOAD)     << ") to load a program from a file\n";
          cout << char(DUMP)     << ") to display a memory dump\n";
          cout << char(RUN)      << ") to run a previously loaded program\n";
          cout << char(QUIT)     << ") to quit this program\n";
          cout << " -> "; cin >> option;
          option = tolower(option);
          switch(option)
          {
              default: cout << option << " is not a valid option!!!\n"; break;
              case CLEAR:
              case MEMORY:
              case REGISTERS:
              case LOAD:
              case DUMP:
              case RUN:
              case QUIT: ok = true;
          }
    }
    return (optionType) option;
}

When I dump it's supposed to look something like this for memory:
0 1 2 3 4 5 6 7 8 9 A B C D E F
0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
1 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
2 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
3 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
4 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
5 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
6 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
7 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
8 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
9 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
A 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
B 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
C 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
D 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
E 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
F 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

Then I display the registers:
R1: 00
.
.
.
RF: 00

PC: 00
IR: 0000

Memory gets its instructions from loading the .txt file and my run function makes it all work. I'm at a standstill with this hexadecimal thing, and I'm not entirely sure how to make one register represented by two hex digits.

So why not post the most important part of the project -- the input file. If it's binary, we have one set of answers. If it's text we have another set. It helps to have all the info and a complete description. As it is, the description is spread over 3 posts and hidden in the code.

Sorry about the poor clarity in what I've been asking. The input file is a .txt file specified by the user. The file contains a list of 4 hex digit (4 refering to how many digits per instruction) instructions and follow standard machine language opcodes:
0000 No OP
1RXY Load register R with the value of memory location XY; rR = mXY
2RUV Load register R with the value UV; rR = UV
3RXY Store register R to memory location XY; mXY = rR
40SR Copy register S to register R; rR = rS
5RST Add registers S and T and place result in register R; rR = rS + rT
6RST Subtract registers S and T and place result in register R; rR = rS – rT
7RST OR registers S and T and place result in register R; rR = rS OR rT
8RST AND registers S and T and place result in register R; rR = rS AND rT
9RST XOR registers S and T and place result in register R; rR = rS XOR rT
A1RU rotate the bits in register R, U places to the right; rR = rR rotated U places to right
BRUV set the program counter to UV, if the value of
register R is the same as the value of register 0; PC = UV if rR == r0
C000 Halt
D000 No OP
E000 No OP
F000 No OP
I'm not worried about C000-F000.
To interpret each instruction, read the first digit as the operation. A No OP operation means that code is not defined and should be ignored. Any 0 appearing in the remaining digits means that value of that digit is to be ignored and can be any value, Any other digit (1 thru F) is to be read as the required value there. An R, S or T means that digit represents the address of a register. R is the register that receives the value; S and T are registers that provide values. XY indicates the two digits refer to a memory address. U or UV indicate a literal value that is to be used in the instruction. All input will be by loading data from files into specified memory locations before the program begins, and all output will be by viewing a dump of the system after the program halts.
The .txt file will look similiar to the dump field. I'm not worried really about the binary. It's just confusing as to how I represent a register or memory location as a two digit hex number.

So what you posted is exactly the file you are trying to read? Something tells me no. Interesting, but overall useless.

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.