Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read in binary data and cast to unsigned char (C++)

I have a raw image file that is saved in binary data (no encoding). I want to read in the file and cast the values to an unsigned char. But I'm not sure how to begin going about doing this. Each file contains 640x480 bytes. Each pixel is 8bits.

I've used the C++ help page here: http://www.cplusplus.com/doc/tutorial/files/, however when I am couting the data, it seems to be showing the same binary/non-human readable characters. Can someone please advise? Here is my code so far:

    #include <iostream>
    #include <fstream>
    using namespace std;

ifstream::pos_type size;
char * memblock;

int main () {
  ifstream file ("imageData.raw", ios::in|ios::binary|ios::ate);
  if (file.is_open())
  {
    size = file.tellg();
    memblock = new char [size];
    file.seekg (0, ios::beg);
    file.read (memblock, size);

    cout << "the complete file content is in memory";

    for (int l=0; l<size; l++){
    cout << " memblock[] is = " << (unsigned char)memblock[l] << " index was l = " << l << endl;
    }

    file.close();


    delete[] memblock;
  }
  else cout << "Unable to open file";
  return 0;
}

Here is a sample capture of the output:

 memblock[] is = ▀ index was l = 2937
 memblock[] is = E index was l = 2938
 memblock[] is = τ index was l = 2939
 memblock[] is = C index was l = 2940
 memblock[] is = ┘ index was l = 2941
 memblock[] is = B index was l = 2942
 memblock[] is = ╬ index was l = 2943
 memblock[] is = D index was l = 2944
 memblock[] is = ┼ index was l = 2945
 memblock[] is = C index was l = 2946
 memblock[] is = ╝ index was l = 2947
 memblock[] is = B index was l = 2948
 memblock[] is = ┤ index was l = 2949
 memblock[] is = B index was l = 2950
 memblock[] is = ¿ index was l = 2951
 memblock[] is = > index was l = 2952
 memblock[] is = í index was l = 2953
 memblock[] is = ; index was l = 2954
 memblock[] is = £ index was l = 2955
 memblock[] is = 6 index was l = 2956
 memblock[] is = á index was l = 2957
 memblock[] is = 4 index was l = 2958
 memblock[] is = Ñ index was l = 2959
 memblock[] is = 7 index was l = 2960
 memblock[] is = ╡ index was l = 2961
like image 850
c0d3rz Avatar asked Jun 26 '12 19:06

c0d3rz


People also ask

How to represent unsigned char in C?

So it means that the range of unsigned char data type ranges from 0 to 255. Example: unsigned char ch = 'a'; Initializing an unsigned char: Here we try to insert a char in the unsigned char variable with the help of ASCII value.

What is the format specifier for unsigned char in c language?

printf("%u",x);

Why do we use unsigned char in C?

The basic ASCII values are in range 0 to 127. The rest part of the ASCII is known as extended ASCII. Using char or signed char we cannot store the extended ASCII values. By using the unsigned char, we can store the extended part as its range is 0 to 255.

Why do we need signed and unsigned char?

While the char data type is commonly used to represent a character (and that's where it gets its name) it is also used when a very small amount of space, typically one byte, is needed to store a number. A signed char can store a number from -128 to 127, and an unsigned char can store a number from 0 to 255.


1 Answers

unsigned char* memblock; // change declaration of memblock

...

memblock = new unsigned char[size]; // change to unsigned char
file.seekg (0, ios::beg);
file.read ((char*)memblock, size); // cast to a char* to give to file.read

To print numeric values instead of characters, cast to an int before printing.

(int) memblock[l]
like image 146
trumank Avatar answered Sep 19 '22 00:09

trumank