Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Convert image to byte array

Posted on 2014-12-03
8
Medium Priority
?
288 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 25

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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
LVL 35

Accepted Solution

by:
sarabande earned 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Six Sigma Control Plans
Starting up a Project

661 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