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?
#include <iostream>
#include <fstream>
#include <string>
int main () {
std::ifstream is ("test.png", std::ifstream::binary);
unsignedchar array[4][4];
if (is) {
is.seekg (0, is.end);
int length = is.tellg();
is.seekg (0, is.beg);
char * buffer = newchar [length];
is.read (buffer,length);
//Here I get the error
unsignedchar * 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;
};
};
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.
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;
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.
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
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.