StarDusterII
asked on
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.
I think you may be able to use the code taken from
http://rdsrc.us/0NJuov
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
}
ASKER
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.
Can't use the debugger because this need to write this in an existing log function.
>> 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...
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);
}
ASKER
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));
short out[40];
... fill out array with data...
DEBUG("Converted Values: %s", shortToString(out));
ASKER
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
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
I'll fix the spacing and make it short.
oops, talking to evilrix - never mind. I will work on your shortToString() function.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>> I will work on your shortToString() function.
You mean mine isn't enough? :)
You mean mine isn't enough? :)
I will not work on the shortToString() function
>> 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.
I was kidding... work away :) There's always room for improving things and there are many ways to achieve the same result.
ASKER
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
OUT: 0000 FFFFFFF0 FFFFFFF0 FFFFFFE0 0000 0040 0020 0030 0040 0030 0030 0040 0060 0040 0050 0040 0050 0040
For this, only one way is needed. Just give the desired char * return value if it is important to StarDusterII.
>> 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 *.
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 *.
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?