Hi T,
Do you have a C Code that simply reads hex values of BMP image?
panic mode suggested IrfanView which can view any file as a nice hex table. Try it.
https://www.irfanview.com/
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Hi T,
Do you have a C Code that simply reads hex values of BMP image?
This apps is great.misterT said:panic mode suggested IrfanView which can view any file as a nice hex table. Try it.
https://www.irfanview.com/
/* fread example: read an entire file */
#include <stdio.h>
#include <stdlib.h>
int main () {
FILE* pFile;
long lSize;
char* buffer;
size_t result;
/* Open the file for reading (r) in binary mode (b) */
pFile = fopen("myfile.bin", "rb"); // rb = read in binary mode
/* Check that file open was successful */
if (pFile==NULL) {fputs("File error",stderr); exit (1);}
// obtain file size:
fseek(pFile,0,SEEK_END);
lSize = ftell(pFile);
rewind(pFile);
// allocate memory to contain the whole file:
buffer = (char*) malloc(sizeof(char)*lSize);
/* Check that memory allocation was successful */
if (buffer == NULL) {fputs("Memory error",stderr); exit (2);}
// copy the file into the buffer:
result = fread(buffer,1,lSize,pFile);
if (result != lSize) {fputs ("Reading error",stderr); exit (3);}
/* the whole file is now loaded in the memory buffer. */
/* You can access the file byte by byte: */
buffer[0]; // First byte of the file
buffer[1]; // second byte of the file
// etc. up to:
buffer[lSize-1]; // last byte of the file
// This prints the file as hex values, byte by byte
for(long i=0; i<lSize; i++)
{
if(!(i%16)) { printf("\n"); }
printf("%02X ", buffer[i]);
}
// terminate
fclose (pFile);
free (buffer);
return 0;
}
Are you saying that the program output is different than what irfanview shows? Or is the .bmp file format different from what you expect? Can you post the test image you are using?
/* fread example: read an entire file */
#include <stdio.h>
#include <stdlib.h>
void printHex(char value);
void printHexNible(char value);
int main () {
FILE* pFile;
long lSize;
char* buffer;
size_t result;
/* Open the file for reading (r) in binary mode (b) */
pFile = fopen("myfile.bin", "rb"); // rb = read in binary mode
/* Check that file open was successful */
if (pFile==NULL) {fputs("File error",stderr); exit (1);}
// obtain file size:
fseek(pFile,0,SEEK_END);
lSize = ftell(pFile);
rewind(pFile);
// allocate memory to contain the whole file:
buffer = (char*) malloc(sizeof(char)*lSize);
/* Check that memory allocation was successful */
if (buffer == NULL) {fputs("Memory error",stderr); exit (2);}
// copy the file into the buffer:
result = fread(buffer,1,lSize,pFile);
if (result != lSize) {fputs ("Reading error",stderr); exit (3);}
/* the whole file is now loaded in the memory buffer. */
/* You can access the file byte by byte: */
buffer[0]; // First byte of the file
buffer[1]; // second byte of the file
// etc. up to:
buffer[lSize-1]; // last byte of the file
// This prints the file as hex values, byte by byte
for(long i=0; i<lSize; i++)
{
if(!(i%16)) { printf("\n"); }
//printf("%02X ", buffer[i]);
printHEX(buffer[i]);
}
// terminate
fclose (pFile);
free (buffer);
return 0;
}
void printHex(char value)
{
printHexNible(value >> 4);
printHexNible(value & 0x0F);
}
void printHexNible(char value)
{
if (value == 0) {printf("0");}
else if (value == 1) {printf("1");}
else if (value == 2) {printf("2");}
else if (value == 3) {printf("3");}
else if (value == 4) {printf("4");}
else if (value == 5) {printf("5");}
else if (value == 6) {printf("6");}
else if (value == 7) {printf("7");}
else if (value == 8) {printf("8");}
else if (value == 9) {printf("9");}
else if (value == 10) {printf("A");}
else if (value == 11) {printf("B");}
else if (value == 12) {printf("C");}
else if (value == 13) {printf("D");}
else if (value == 14) {printf("E");}
else if (value == 15) {printf("F");}
}
[/COLOR]
irfanview does not read FFs at all (I cannot copy what it reads)
I'll write down the first 16 bytes irfanview reads:
42 4d b6 07 00 00 00 00
00 00 36 04 00 00 28 00
I'm beginning to think there's something wrong with DEV-C++ (I used an empy project in C).
Yes, I too think that there is a bug in the printf-function when printing hex values.
The Irfanview output seems to be correct:
The first two bytes are 42 4d, which in ascii is "BM".. so that is correct.
Note that the "file size (4 bytes)" is in "Little Endian". So, the "B6 07 00 00" is a hex value: 0x000007B6
.. this is a file size of 1974 bytes. Does this seem to be correct?
As you can see in irfanview, now that you got the bmp file (in rar), it gives different reading.
I mean, you fill a programmer position?