C function to convert signed short array to hex string

I would like a function to return a string of hex characters that represent  the contents of a signed short array in C for debugging purposes.
StarDusterIIAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
evilrixConnect With a Mentor Senior Software Engineer (Avast)Commented:
>> Here's the output from your example.

The output I see is 1 5 9 A 27 257 4DD

If you want it formatted as full hex values with leading 0's try the code below, which gives me this...

0001 0005 0009 000A 0027 0257 04DD


#include <stdio.h>  

void short2hex(short const * a, int const asize, char * s, int const ssize)
{
   int i = 0;
   int p = 0;

   for(; (i < asize) && (p < ssize) ; ++i)
   {
      p+=sprintf(&s[p], "%04X ", a[i]);
   }
}

int main()
{
   short a[] = { 1, 5, 9, 10, 39, 599, 1245 };
   char s[100] = {0};

   short2hex(a, sizeof(a)/sizeof(a[0]), s, sizeof(s)/sizeof(s[0]));

   puts(s);
}

Open in new window

0
 
evilrixSenior Software Engineer (Avast)Commented:
You can use printf to display the hex representation of a numeric value.
http://www.cplusplus.com/reference/clibrary/cstdio/printf/

Just wrap this in a function to do what you need.

>> debugging purposes.
Why not just use a debugger?
0
 
phoffricCommented:
I think you may be able to use the code taken from
    http://rdsrc.us/0NJuov
#include <stdio.h>  
  
unsigned int numbers[100];  
char hex[100][9]; // room for 8 chars and the terminating NULL  
  
// fill 'numbers'  
  
// convert:  
  
for (int i = 0; i < 100; ++i) {  
  
  sprintf(hex[i],"%x",numbers[i]); // '%x' will format as hex  
}

Open in new window

0
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.

 
StarDusterIIAuthor Commented:
Trying to modify a C program and never written C so not sure what the printf statement should like for an array of shorts.  So, I need an explicit example of a function to return a string of hex characters from a short array.

Can't use the debugger because this need to write this in an existing log function.
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> So, I need an explicit example of a function to return a string of hex characters from a short array.

Ok, something quick and dirty to get your going...
#include <stdio.h>  

void short2hex(short const * a, int const asize, char * s, int const ssize)
{
   int i = 0;
   int p = 0;

   for(; (i < asize) && (p < ssize) ; ++i)
   {
      p+=sprintf(&s[p], "%X ", a[i]);
   }
}

int main()
{
   short a[] = { 1, 5, 9, 10, 39, 599, 1245 };
   char s[100] = {0};

   short2hex(a, sizeof(a)/sizeof(a[0]), s, sizeof(s)/sizeof(s[0]));

   puts(s);
}

Open in new window

0
 
StarDusterIIAuthor Commented:
phoffric,  I have an array of "short" (two bytes) that I need to display.  Your example uses unsigned int's.  Will that work the same way?  I was looking for something like:

short out[40];

... fill out array with data...

DEBUG("Converted Values: %s", shortToString(out));

0
 
StarDusterIIAuthor Commented:
Evilrix,

Here's the output from your example.  Does the spacing make sense to you?

OUT: 320 320 480 470 370 270 2B0 2D0 1D0 280 FFFFFFA0 FFFFFDD0 FFFFFCD0 FFFFFD10 FFFFFD50 FFFFFD90 FFFFFD90
0
 
phoffricCommented:
I'll fix the spacing and make it short.
0
 
phoffricCommented:
oops, talking to evilrix - never mind. I will work on your shortToString() function.
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> I will work on your shortToString() function.

You mean mine isn't enough? :)
0
 
phoffricCommented:
I will not work on the shortToString() function
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> I will not work on the shortToString() function

I was kidding... work away :) There's always room for improving things and there are many ways to achieve the same result.
0
 
StarDusterIIAuthor Commented:
Evilrix, Interesting...  doesn't seem to like the FFFF's.  ;-)

OUT: 0000 FFFFFFF0 FFFFFFF0 FFFFFFE0 0000 0040 0020 0030 0040 0030 0030 0040 0060 0040 0050 0040 0050 0040

0
 
phoffricCommented:
For this, only one way is needed. Just give the desired char * return value if it is important to StarDusterII.
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> doesn't seem to like the FFFF's

That's cos they're negative -- those values are actually correct as hex values for signed short. Change it to unsigned short and you'll get the hex representation you are expecting (you'll also need to change short2hex to take an unsigned short const *.
0
All Courses

From novice to tech pro — start learning today.