Reading hex data from a .bin file returns FFs at the start of the string

45 Views Asked by At

I am trying to read the HEX data of a .bin file to a string. At the start of the string there is a bunch of FFs and after that the actual data starts. I want to only read the data that the .bin file contains.

This is my code:

#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <vector>


std::string filePath;
std::string hexData;

std::stringstream hexStream;

int main()
{
    std::cout << "Enter file path to the BIOS dump: ";
    std::cin >> filePath;
    std::ifstream file(filePath, std::ios::binary);
    std::vector<unsigned char> buffer(std::istreambuf_iterator<char>(file), {});
    file.close();
    for (unsigned char byte : buffer) {
        hexStream << std::hex << std::setw(2) << std::setfill('0') << static_cast<unsigned int>(byte);
    }
    hexData = hexStream.str();
    std::cout << hexData;

    return 0;
}
0

There are 0 best solutions below