User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 391,596 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,706 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 4852 | Replies: 7
Reply
Join Date: Aug 2005
Posts: 10
Reputation: Sutanu is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Sutanu Sutanu is offline Offline
Newbie Poster

unsigned character pointer to Hex values

  #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).

char* m_cHexvalue = new char[2*BufSize +1];
char *m_cCurrCharVal=new char[3];

*(m_cCurrCharVal+2)=0X00;
memset(m_cHexvalue ,0,2);

for(int count=0;count<BufSize ;count++)
{
     memset(m_cCurrCharVal,0,2);
     sprintf(m_cCurrCharVal,"%x",*(m_cpSockData+count));
     strcat(m_cHexvalue ,m_cCurrCharVal);

}
<< 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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2004
Posts: 3,449
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 16
Solved Threads: 138
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: unsigned character pointer to Hex values

  #2  
Aug 19th, 2005
This is pretty much the same, but it's all I've got at the moment.
#include <stdio.h>

char *foo(char *dst, const char *src)
{
   char *start = dst;
   while ( *src )
   {
      dst += sprintf(dst, "%x", (unsigned)*src++);
   }
   return start;
}

char *bar(char *dst, const char *src)
{
   char *start = dst;
   while ( *src )
   {
      unsigned value;
      if ( sscanf(src, "%2x", &value) == 1 )
      {
         src += 2;
         *dst++ = value;
      }
   }
   *dst = '\0';
   return start;
}

int main(void)
{
   const char abc[] = "ABC";
   char over [ sizeof abc * 2  - 1 ], back [ sizeof abc ];
   printf("\"%s\" -> \"%s\"\n", abc,  foo(over, abc));
   printf("\"%s\" -> \"%s\"\n", over, bar(back, over));
   return 0;
}

/* my output
"ABC" -> "414243"
"414243" -> "ABC"
*/
Last edited by Dave Sinkula : Aug 19th, 2005 at 6:27 pm. Reason: Added 'bar'.
Reply With Quote  
Join Date: Aug 2005
Posts: 10
Reputation: Sutanu is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Sutanu Sutanu is offline Offline
Newbie Poster

Re: unsigned character pointer to Hex values

  #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  
Join Date: Apr 2004
Posts: 3,449
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 16
Solved Threads: 138
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: unsigned character pointer to Hex values

  #4  
Aug 19th, 2005
Perhaps discard the standard routines and roll your own.
#include <stdio.h>
#include <ctype.h>

unsigned char *foo(unsigned char *dst, const unsigned char *src)
{
   static const unsigned char nibble[] = 
   {
      '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
   };
   unsigned char *start = dst;
   while ( *src )
   {
      *dst++ = nibble [ *src >> 4    ];
      *dst++ = nibble [ *src++ & 0xF ];
   }
   *dst = '\0';
   return start;
}

unsigned char *bar(unsigned char *dst, const unsigned char *src)
{
   unsigned char *start = dst;
   while ( *src )
   {
      if ( isdigit(*src) )
      {
         *dst = *src++ - '0';
      }
      else
      {
         *dst = *src++ - 'A' + 10;
      }
      *dst <<= 4;
      if ( isdigit(*src) )
      {
         *dst++ += *src++ - '0';
      }
      else
      {
         *dst++ += *src++ - 'A' + 10;
      }
   }
   *dst = '\0';
   return start;
}

int main(void)
{
   const unsigned char abc[] = "ABCMNO";
   unsigned char over [ sizeof abc * 2  - 1 ], back [ sizeof abc ];
   printf("\"%s\" -> \"%s\"\n", abc,  foo(over, abc));
   printf("\"%s\" -> \"%s\"\n", over, bar(back, over));
   return 0;
}

/* my output
"ABCMNO" -> "4142434D4E4F"
"4142434D4E4F" -> "ABCMNO"
*/
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 19th, 2005 at 11:23 pm. Reason: Got rid of temporary variables.
Reply With Quote  
Join Date: Aug 2005
Posts: 10
Reputation: Sutanu is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Sutanu Sutanu is offline Offline
Newbie Poster

Re: unsigned character pointer to Hex values

  #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  
Join Date: Apr 2004
Posts: 3,449
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 16
Solved Threads: 138
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: unsigned character pointer to Hex values

  #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.
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,546
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 860
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Most Valuable Poster

Re: unsigned character pointer to Hex values

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

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

Re: unsigned character pointer to Hex values

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

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 11:33 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC