unsigned character pointer to Hex values

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2005
Posts: 10
Reputation: Sutanu is an unknown quantity at this point 
Solved Threads: 0
Sutanu Sutanu is offline Offline
Newbie Poster

unsigned character pointer to Hex values

 
0
  #1
Aug 19th, 2005
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).

  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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,452
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 251
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: unsigned character pointer to Hex values

 
0
  #2
Aug 19th, 2005
This is pretty much the same, but it's all I've got at the moment.
  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'.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 10
Reputation: Sutanu is an unknown quantity at this point 
Solved Threads: 0
Sutanu Sutanu is offline Offline
Newbie Poster

Re: unsigned character pointer to Hex values

 
0
  #3
Aug 19th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,452
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 251
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: unsigned character pointer to Hex values

 
0
  #4
Aug 19th, 2005
Perhaps discard the standard routines and roll your own.
  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.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 10
Reputation: Sutanu is an unknown quantity at this point 
Solved Threads: 0
Sutanu Sutanu is offline Offline
Newbie Poster

Re: unsigned character pointer to Hex values

 
0
  #5
Aug 20th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,452
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 251
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: unsigned character pointer to Hex values

 
0
  #6
Aug 20th, 2005
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.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,625
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1495
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: unsigned character pointer to Hex values

 
0
  #7
Aug 20th, 2005
  1. if( *src <= '9' )

what if *src < '0' ? isdigit() will catch that, but your if statement won't.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 10
Reputation: Sutanu is an unknown quantity at this point 
Solved Threads: 0
Sutanu Sutanu is offline Offline
Newbie Poster

Re: unsigned character pointer to Hex values

 
0
  #8
Aug 20th, 2005
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).
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC