C++ - Std::Hex is not returning hexadecimal value

Asked

Viewed 107 times

0

I am trying to develop a Gameboy emulator as a challenge to myself, I am in the process of decoding the opcodes, for this I need to get the hexadecimal value of it extracted from the game ROM, but instead of returning an Hex value it returns strange characters, like in the photo:

    void CPU::Exec()
    {

        std::uint8_t opcode = mmu->ReadMemory(PC);

        switch (opcode) {

        case 0x0:
            cycles += 4;
            PC++;
            break;

        }

        std::cout << "Current opcode: 0x" << std::hex << std::uppercase << opcode << std::endl;

    }

caractere estranho

The function ReadMemmory basically reading from the ROM which is basically a uint8_t vector that stores the contents of the ROM

    bool Cartridge::Load()
    {

        std::fstream file(gamePath, std::ios::in | std::ios::binary);
        if (!file.is_open()) {
            std::cerr << "Error: Could not read file" << std::endl;
            return false;
        }

        this->romData = std::vector<std::uint8_t>(std::istreambuf_iterator<char>(file), {});

        file.close();

        return true;
    }

1 answer

1


The reason is that the cout is treating the guy std::uint8_t as if it were a char when printing, rather than a unsigned int. Because of this so much the std::hex as to the std::uppercase has no effect. The character you are seeing is the character of the ascii table corresponding to the value of the read option.

Let’s show this with a very simple program.

#include <iostream>

int main(int argc, char *argv[])
{
    std::uint8_t a = 77;

    std::cout << std::hex << "O valor em é " << static_cast<unsigned int>(a) << std::endl;
    return 0;
}

This program will display the message "The value is M", when the expected value would be "The value is 4d" (4d is the base value 16 of 77). Like the cout dealt with the variable a like she’s the type char, then displayed the character corresponding to the number "77" in the ascii table, which is the letter "M".

To solve just cast a variable to be printed to unsigned int and the correct hexadecimal value will be printed.

If you do not want to cast you can use the printf, since the printf will interpret and print the variable value depending on the formatting string you use and not the variable type.

With the printf would be

#include <cstdint>
#include <cstdio>

int main(int argc, char *argv[])
{
    std::uint8_t a = 77;

    printf("O valor em é %X", a);
    return 0;
}

Edit

I looked here in my system, seeing where the guy std::uint8_t is defined, and in fact it is nothing more than a typedef of a unsigned char.

  • I thought Hex would return hexadecimal value of any type

  • std::dec, std::hex, std::oct https://en.cppreference.com/w/cpp/io/manip/hex "Modifies the default Numeric base for integer I/O." Only of numerical and integer types.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.