Solved

Convert image to byte array

Posted on 2014-12-03
8
208 Views
Last Modified: 2015-05-28
I would like some code or worst case a utility that can take an image, .jpeg, .bmp, .... and convert it to it's hexidecimal representation in an array. Code wise just straight c. NO C##, VB, etc.

Thank You
0
Comment
Question by:atomicgs12
8 Comments
 
LVL 24

Expert Comment

by:chaau
ID: 40479877
If C++ is an option and it is a Windows environment then have a look at CImage class. Requires MFC.

Otherwise, have a look at GOCR library. The library itself is a OCR library. You do not have to use it, however there are two files there pnm.h and pnm.c that can handle many of known image files. have a look at readpnm() function there. I have used this library for OCR purposes in one of my projects. You can just use the pnm files, I guess, check its license.
0
 

Author Comment

by:atomicgs12
ID: 40480910
Thanks but only C, I tried to cover all the languages not to use guess I have to make my list longer.
No C#, No VB, No C++, No Python, No Pascal, No Fortron, No HTML, No Silverlight, No...
0
 

Author Comment

by:atomicgs12
ID: 40480961
Looking at GOCR file pnm.c I don't see a ' readpnm() ' I do see a 'readpgm()' which I'm assuming your referring to. Even in that function I don't see an explanation as to to the input variables. I'm assuming 'char *name' is the name of the picture file, put I don't know what 'pix *p' and 'int vvv' are referring to.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 33

Accepted Solution

by:
sarabande earned 500 total points
ID: 40481391
you may use the following code:

int binfile2hexstring1(const char * inputfile, char hexbuf[], int sizbuf)
{
     char hexdigits[] = "0123456789ABCDEF";
     struct stat fs = { 0 }; 
     FILE * file;
     char * buf;
     int nread;
     char ac[3];
     int n;
     char * p, * q;

     if (stat(inputfile, &fs) != 0 || fs.st_size == 0)
          return errno;   // file does not exist or is empty
      if (sizbuf < 2*fs.st_size)
           return -1;  // hex buffer to small
      file = fopen(inputfile, "r");
      buf =  (char*)calloc(fs.st_size);
      if (file == NULL ||buf == NULL)
            return errno;
     if ((nread = fread(&buf[0], 1, fs.st_size, file)) < (int)fs.st_size)
          return errno;  // invalid read
     fclose(file);
     p = &hexbuf[0];
     q = &buf[0];
     for (n = 0; n < (int)fs.st_size; ++n)    
     {
       *p++ = hexdigits[((*q)&0xf0)>>4];  // convert high half-byte
       *p++ = hexdigits[((*q)&0x0f)];         // convert low half-byte
         q++;
     }     
     return 0;
}

Open in new window


the code is pure c "derived" from an older question where we not only discussed conversion to hex but also differences in speed using c or c++ solutions.
 
see http://www.experts-exchange.com/Programming/Languages/CPP/Q_28457320.html for more.

Sara
0
 

Expert Comment

by:William Sherif
ID: 40510293
I would use SDL https://www.libsdl.org/.

Load the image using IMG_Load (or if you're using a png, use the SDL_Image 2.0 library).

After you load the image, you can inspect the contents of the surface using the pixels member of the SDL_Surface object (see https://wiki.libsdl.org/SDL_Surface).

If you really want a hold-your-hand start up tutorial, check out my udemy course https://www.udemy.com/creating-games-from-scratch-using-sdl-2/
0
 

Author Comment

by:atomicgs12
ID: 40707609
Need to try the suggested solution before selecting a correct answer(s).
Thank you all
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about problems I had with the new location API and permissions in Marshmallow
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question