Solved

Convert image to byte array

Posted on 2014-12-03
8
224 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 34

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

733 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