I'm trying to read a JPG image file and convert it to string of hex code (not hex of pixel) in C.
Something like:
FFD8FFE000114A464946000102030405060708090AFFDB00...
.
I tried many way but not working. Someone has any idea?
My code which I tried with stb libraries: https://codeload.github.com/nothings/stb/zip/master
// USAGE: gcc -std=c99 image.c -o image -lm
#include <stdio.h>
#include <math.h>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
const size_t NUM_PIXELS_TO_PRINT = 10U;
int main(void) {
int width, height, comp;
unsigned char *data = stbi_load("r3.jpg", &width, &height, &comp, 0);
if (data) {
printf("width = %d, height = %d, comp = %d (channels)\n", width, height, comp);
for (size_t i = 0; i < NUM_PIXELS_TO_PRINT * comp; i++) {
printf("%02x%s", data[i], ((i + 1) % comp) ? "" : "\n");
}
printf("\n");
}
return 0;
}
The error I got when try with John Smith:
ImageProcess.c: In function ‘main’:
ImageProcess.c:14:5: warning: implicit declaration of function ‘bzero’ [-Wimplicit-function-declaration]
bzero(data, fsize + 1);
^
ImageProcess.c:18:5: warning: implicit declaration of function ‘hexlifyn’ [-Wimplicit-function-declaration]
char* yourDataStr = hexlifyn((char*)data, (uint)fsize);
^
ImageProcess.c:18:48: error: ‘uint’ undeclared (first use in this function)
char* yourDataStr = hexlifyn((char*)data, (uint)fsize);
^
ImageProcess.c:18:48: note: each undeclared identifier is reported only once for each function it appears in
ImageProcess.c:18:53: error: expected ‘)’ before ‘fsize’
char* yourDataStr = hexlifyn((char*)data, (uint)fsize);
^
ImageProcess.c: At top level:
ImageProcess.c:21:28: error: unknown type name ‘uint’
char *hexlifyn(char *bstr, uint str_len) {
^