943,944 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 13315
  • C++ RSS
Aug 19th, 2005
0

unsigned character pointer to Hex values

Expand Post »
Dear all:

I've created TCPsocket in linux using C++. It receives an array of unsigned characters. I need the corresponding hex values in character format.
i.e. "ABC" in hex it is "414243". Now I want to treat each of "414243" as character.

So I have done the coding as follow ( taking into cosideration that the TCP socket is established and the data is received in an unsigned character pointer m_cpSockData sized same to Buffer Size of the socket. And the number of byte read is m_iReadLen).

C++ Syntax (Toggle Plain Text)
  1. char* m_cHexvalue = new char[2*BufSize +1];
  2. char *m_cCurrCharVal=new char[3];
  3.  
  4. *(m_cCurrCharVal+2)=0X00;
  5. memset(m_cHexvalue ,0,2);
  6.  
  7. for(int count=0;count<BufSize ;count++)
  8. {
  9. memset(m_cCurrCharVal,0,2);
  10. sprintf(m_cCurrCharVal,"%x",*(m_cpSockData+count));
  11. strcat(m_cHexvalue ,m_cCurrCharVal);
  12.  
  13. }
<< moderator edit: added [code][/code] tags >>


I also have similar thing while writing back to the socket.

But this reduces the efficiency of the code..

Can any body help to provide a mechanism where efficiency can be increased.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Sutanu is offline Offline
10 posts
since Aug 2005
Aug 19th, 2005
0

Re: unsigned character pointer to Hex values

This is pretty much the same, but it's all I've got at the moment.
C++ Syntax (Toggle Plain Text)
  1. #include <stdio.h>
  2.  
  3. char *foo(char *dst, const char *src)
  4. {
  5. char *start = dst;
  6. while ( *src )
  7. {
  8. dst += sprintf(dst, "%x", (unsigned)*src++);
  9. }
  10. return start;
  11. }
  12.  
  13. char *bar(char *dst, const char *src)
  14. {
  15. char *start = dst;
  16. while ( *src )
  17. {
  18. unsigned value;
  19. if ( sscanf(src, "%2x", &value) == 1 )
  20. {
  21. src += 2;
  22. *dst++ = value;
  23. }
  24. }
  25. *dst = '\0';
  26. return start;
  27. }
  28.  
  29. int main(void)
  30. {
  31. const char abc[] = "ABC";
  32. char over [ sizeof abc * 2 - 1 ], back [ sizeof abc ];
  33. printf("\"%s\" -> \"%s\"\n", abc, foo(over, abc));
  34. printf("\"%s\" -> \"%s\"\n", over, bar(back, over));
  35. return 0;
  36. }
  37.  
  38. /* my output
  39. "ABC" -> "414243"
  40. "414243" -> "ABC"
  41. */
Last edited by Dave Sinkula; Aug 19th, 2005 at 7:27 pm. Reason: Added 'bar'.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Aug 19th, 2005
0

Re: unsigned character pointer to Hex values

Thanks for the reply.

I understand you have removed some operations. That reduces quite a big time.

I think this can further be minimized. Do you have any idea of doing this with the help of asm codes in C.

I've not used so far but I think that may help the roundtrip time.

Thanks again.
Sutanu
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Sutanu is offline Offline
10 posts
since Aug 2005
Aug 19th, 2005
0

Re: unsigned character pointer to Hex values

Perhaps discard the standard routines and roll your own.
C++ Syntax (Toggle Plain Text)
  1. #include <stdio.h>
  2. #include <ctype.h>
  3.  
  4. unsigned char *foo(unsigned char *dst, const unsigned char *src)
  5. {
  6. static const unsigned char nibble[] =
  7. {
  8. '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
  9. };
  10. unsigned char *start = dst;
  11. while ( *src )
  12. {
  13. *dst++ = nibble [ *src >> 4 ];
  14. *dst++ = nibble [ *src++ & 0xF ];
  15. }
  16. *dst = '\0';
  17. return start;
  18. }
  19.  
  20. unsigned char *bar(unsigned char *dst, const unsigned char *src)
  21. {
  22. unsigned char *start = dst;
  23. while ( *src )
  24. {
  25. if ( isdigit(*src) )
  26. {
  27. *dst = *src++ - '0';
  28. }
  29. else
  30. {
  31. *dst = *src++ - 'A' + 10;
  32. }
  33. *dst <<= 4;
  34. if ( isdigit(*src) )
  35. {
  36. *dst++ += *src++ - '0';
  37. }
  38. else
  39. {
  40. *dst++ += *src++ - 'A' + 10;
  41. }
  42. }
  43. *dst = '\0';
  44. return start;
  45. }
  46.  
  47. int main(void)
  48. {
  49. const unsigned char abc[] = "ABCMNO";
  50. unsigned char over [ sizeof abc * 2 - 1 ], back [ sizeof abc ];
  51. printf("\"%s\" -> \"%s\"\n", abc, foo(over, abc));
  52. printf("\"%s\" -> \"%s\"\n", over, bar(back, over));
  53. return 0;
  54. }
  55.  
  56. /* my output
  57. "ABCMNO" -> "4142434D4E4F"
  58. "4142434D4E4F" -> "ABCMNO"
  59. */
This is for ASCII, and I think it is right. But I was wrong with it a minute ago.
Last edited by Dave Sinkula; Aug 20th, 2005 at 12:23 am. Reason: Got rid of temporary variables.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Aug 20th, 2005
0

Re: unsigned character pointer to Hex values

Thanks again.

I performed a dry run. I think it would be better to use

if (*src <= '9')

rather than

if (isdigit(*src))

What do you think...

Sutanu
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Sutanu is offline Offline
10 posts
since Aug 2005
Aug 20th, 2005
0

Re: unsigned character pointer to Hex values

Quote originally posted by Sutanu ...
I performed a dry run. I think it would be better to use

if (*src <= '9')

rather than

if (isdigit(*src))

What do you think...

Sutanu
If that works better, go ahead. Many times the standard functions are implemented faster than writing an equivalent, but not always.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Aug 20th, 2005
0

Re: unsigned character pointer to Hex values

C++ Syntax (Toggle Plain Text)
  1. if( *src <= '9' )

what if *src < '0' ? isdigit() will catch that, but your if statement won't.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Aug 20th, 2005
0

Re: unsigned character pointer to Hex values

Thanks for the concern.

The scenario I'm talking about doesn't permit you to have any values other than 0-9 and A-F.

In other scenarios or to have a generic one I think using a isdigit() is a better option. But then also you can see what happens if any character as * ( ASCII value is 42) comes . Then you probably will have a negative value (42 - 64 +10 = -12).
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Sutanu is offline Offline
10 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: what is classes?
Next Thread in C++ Forum Timeline: Urgent Pls Help: My codes are in errors!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC