Solved

C function to convert signed short array to hex string

Posted on 2011-03-25
15
575 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
[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
  • 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
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!

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

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!

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

756 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