Solved

C function to convert signed short array to hex string

Posted on 2011-03-25
15
569 Views
Last Modified: 2012-05-11
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.
0
Comment
Question by:StarDusterII
  • 6
  • 5
  • 4
15 Comments
 
LVL 40

Expert Comment

by:evilrix
ID: 35219574
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
 
LVL 32

Expert Comment

by:phoffric
ID: 35219601
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
 

Author Comment

by:StarDusterII
ID: 35219617
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
 
LVL 40

Expert Comment

by:evilrix
ID: 35219690
>> 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
 

Author Comment

by:StarDusterII
ID: 35219715
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
 

Author Comment

by:StarDusterII
ID: 35219776
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
 
LVL 32

Expert Comment

by:phoffric
ID: 35219779
I'll fix the spacing and make it short.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 32

Expert Comment

by:phoffric
ID: 35219782
oops, talking to evilrix - never mind. I will work on your shortToString() function.
0
 
LVL 40

Accepted Solution

by:
evilrix earned 500 total points
ID: 35219798
>> 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
 
LVL 40

Expert Comment

by:evilrix
ID: 35219804
>> I will work on your shortToString() function.

You mean mine isn't enough? :)
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35219806
I will not work on the shortToString() function
0
 
LVL 40

Expert Comment

by:evilrix
ID: 35219817
>> 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
 

Author Comment

by:StarDusterII
ID: 35219820
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
 
LVL 32

Expert Comment

by:phoffric
ID: 35219823
For this, only one way is needed. Just give the desired char * return value if it is important to StarDusterII.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 35219835
>> 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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now