Reputation: 105
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) {
^
Upvotes: 0
Views: 7108
Reputation: 267
This will give you image file output as a continuous hex string in your terminal as well as a .txt file:
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
using namespace std;
string myText;
int main()
{
system("xxd -p image.jpg > image.txt | tr -d '\n'");
ifstream MyReadFile("./image.txt");
while (getline (MyReadFile, myText)) {
// Output the text from the file
cout << myText;
}
MyReadFile.close();
return 0;
}
Upvotes: 0
Reputation: 591
If your goal is to get the contents of a file as hex string than that should work:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main() {
char* file_name = "/path/to/any.png";
FILE *f = fopen(file_name, "rb");
if (f==NULL) return;
fseek(f, 0, SEEK_END);
size_t fsize = ftell(f);
fseek(f, 0, SEEK_SET);
void*data = malloc(fsize + 1);
bzero(data, fsize + 1);
fread(data, 1, fsize, f);
fclose(f);
char* yourDataStr = hexlifyn((char*)data, (uint)fsize);
}
char *hexlifyn(char *bstr, uint str_len) {
char *hstr=malloc((str_len*2)+1);
bzero(hstr,(str_len*2)+1);
char *phstr=hstr;
for(int i=0; i<str_len;i++) {
*phstr++ =v2a((bstr[i]>>4)&0x0F);
*phstr++ =v2a((bstr[i])&0x0F);
}
*phstr++ ='\0';
return hstr;
}
char v2a(int c) {
const char hex[] = "0123456789abcdef";
return hex[c];
}
Upvotes: 3
Reputation: 2282
From your comment I understand that you want to retrieve the binary contents of a file (i.e. a JPG image) as a hexadecimal string.
What you're looking for is something called "hex dump". There are various libraries and snippets available that allow doing this with C.
This stackoverflow question addresses exactly this issue.
Upvotes: 1