How can I obtain an hexadecimal value how a string?

I have to read a file and then get his hexadecimal expressed how a string, so I can modify using string modifiers. But ifstream return a vector and I get the error when I use string modifiers. there is any method to get a string variable with hexadecimal value of the file?
I'm using Windows 10 with CodeBlocks and the debugger is set to default.
How can I solve that?

Thanks in advance for every reply.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <fstream>
#include <string>

int main () {
  std::ifstream is ("test.png", std::ifstream::binary);
  unsigned char array[4][4];
  if (is) {

    is.seekg (0, is.end);
    int length = is.tellg();
    is.seekg (0, is.beg);

    char * buffer = new char [length];
    is.read (buffer,length);
    //Here I get the error
    unsigned char * buffer_str=buffer;
    for (int count1=0; count1<4; count1=count1+1)
        {
            for (int count2=0; count2<4; count2=count2+1)
            {
                // Here I get the two errors
                array[count1][count2]=buffer_str.substr(0, 2);
                buffer_str.erase(0, 2)
                };
                };

  return 0;
};
};

Last edited on
I really don't understand what you're asking.

Are you saying that you want to read 16 bytes from the file, and show them as a hex value?
Last edited on
I want to split the buffer vector that contains the hex red from the file to the array declared how an unsigned char. at the end infact I use two string modifiers to split buffer in an array.
I want to split the buffer vector


There is no vector here. We call that an array.

Looks like you want to read an entire file into an array of char, and then get the first sixteen bytes?

Looks like you've mixed up a string with a char array.

buffer_str.erase(0, 2)
Here, buffer_str is a char pointer, but you're trying to use it as if it were a string. It's not a string. A string is created like this:
string a_new_string;
Last edited on
not sure I followed that any better, but this is how you print hex with cout.

cout << hex << array[0][0] << endl;


there is no such thing as a 'hex value' unless you are asking for a character string of the hex from the real value (??).


say array[0][0] contains the value 15 in base 10.
It also, therefore, contains the value f in hex. it means the same thing, its just printed differently, and its the same 'value' internally (in binary as 1111, actually) inside the machine.


do you want the hex string of the value? If that is what you are asking:
stringstream s;
s << hex << array[0][0]; //s is "0x0f" or whatever text it puts in there.



Last edited on
My goal is to split the buffer to an array, but I'm new in C++ so I searched on the internet for a solution to the problem of splitting, but I found only this method that don't work. So for example I have buffer that contains, after the file reading 04A17F9E61003E40C01F20AA95F190CA19BFC6. I want it to be splitted in the array every two values.
array[0][0]=04
array[0][1]=A1
array[0][2]=7F
array[0][3]=9E

array[1][0]=61
array[1][1]=00
array[1][2]=3E
array[1][3]=40

ect...

Then if the buffer is bigger than the array, it will contain the part that remain after the assigment. In this case buffer will contain 19BFC6.
Last edited on
is the file binary? if it is, you are pretty close. You are treating it as such already.

I think you just want

int index = 0;

..for stuff
array[count1][count2]=buffer_str[index++];

then erase the front part of the buffer afterwards in one go.

if the file is actually text, you have more work to do.

Last edited on
I'm just as confused as everyone else -- what's the ultimate goal?

It's already weird that you're reading in a PNG file without using an image processing library. Formatted images are usually read by first processing their special headers, which often contain dimensions and bit depth data, and only then working with the actual image data.

I was expecting to see a png library to help you read .png files, a jpeg library for .jpg, perhaps a function to read in .bmp, etc.
Topic archived. No new replies allowed.