简体   繁体   中英

C++ Convert the binary data read from a file to a char pointer

I am trying to achieve converting a binary content read from a file to a byte continuously. My file samplefile.bin contains 16bytes and this is in the form of binary. In the Hexed.it it will show like this.

在此处输入图像描述

What I needed in this case is I need to extract these binary values in Hex format and store it to a char *

I will post my sample code over here.

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

using namespace std;

int main()
{
   uint8_t *ptr;
   long size;
   ifstream file ("samplefile.bin", ios::in|ios::binary|ios::ate);
   size = file.tellg();
   std::vector<uint8_t> memblock(size);
   file.seekg (0, ios::beg);
   file.read(reinterpret_cast<char*>(&memblock[0]), size);
   file.close();
   ptr = new uint8_t[size];
   int z=0;
   for(int i=0; i < memblock.size(); i++)
   {
     std::cout << memblock.at(i) << ' ';
     z=memblock.at(i)&0xff;
     ptr[i] = z;
   }

   cout<<"Output"<<endl;
   for (int i=0;i<memblock.size();i++)
   {
       cout<<ptr[i];
   }
   delete []ptr;

return 0;

}

I want the ptr to be passed to some other function. But the output of this print cout<<ptr[i]<<endl; is like below which means the conversion is not happening.

╠ ↕ ‼ ¶ ╙ O N ╥ í * : J Z [ \ J

I want something like below

ptr[0] = 0xCC
ptr[1] = 0x12
...
ptr[15] = 0x4A

When I give like this cout<<(int)ptr[i]<<endl; , I am getting the decimal value of 0xCC as 204 and the same thing for other prints.

I already referred to C++ read binary file and convert to hex and Convert binary file to hex notation . I didn't find the exact solution because in these two links they are converting it to string. My intention is not this one.

I referred to this How properly to read data from binary file to char array which looks similar to mine. But it didn't solve my issue.
I want this 0xCC as a single byte and this should be stored to ptr[0] and similarly for the other values

How to workaround this issue? Am I missing something here? I am a newbie in C++ and please help me to resolve this issue

Appreciating the efforts.

Read file and copy its data to vector of char. You can use iomanip like std::hex or std::uppercase to print integer value into hex.

Allocating and passing raw pointer to another function is not recommended. Simply pass a vector of char or use unique_ptr, they'll prevent you from memory problems.

#include <iostream>
#include <iomanip>

std::ifstream file("samplefile.bin", ios::binary);
auto mem = std::vector<char>(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>());
for (char c : mem)
{
    std::cout << std::uppercase << std::hex << static_cast<int>(c) << endl;
}

There are multiple problems in your code, most fundamentally the fact that you seem to expect std::byte8_t to be represented as a hexdump by default when fed to an output stream. This is not the case. You need to explicitly tell the stream to perform the necessary formatting for you. And if you want to store that representation, you'll need to also store the streamed result.

Many roads lead to Rome, and unfortunately the C++ standard IO stream library doesn't make working with byte streams very ergonomic. But here's one approach:

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

using byte = std::uint8_t;

auto read_bytes(std::istream& is, std::vector<byte>& bytes) -> std::istream& {
    return is.read(& reinterpret_cast<char&>(bytes[0]), bytes.size());
}

auto hexdump(std::vector<byte> const& bytes) -> std::string {
    auto ostr = std::ostringstream();
    ostr << std::uppercase << std::hex << std::setw(2) << std::setfill('0');
    for (auto const c : bytes) {
        ostr << int{c};
    }
    return ostr.str();
}

int main(int argc, char** argv) {
    if (argc != 2) return 1;

    auto fs = std::ifstream(argv[1], std::ios_base::binary | std::ios_base::ate);
    auto const size = fs.tellg();
    fs.seekg(0);
    auto buffer = std::vector<byte>(size);
    if (not read_bytes(fs, buffer)) return 1;

    std::cout << hexdump(buffer) << "\n";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM