convert char to hexadecimal

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

Join Date: Apr 2008
Posts: 29
Reputation: savinki is an unknown quantity at this point 
Solved Threads: 0
savinki savinki is offline Offline
Light Poster

convert char to hexadecimal

 
0
  #1
Apr 20th, 2008
i want to convert char array element into hexadecimal value. But the code i wrote is not giving the output as i expect.
  1. unsigned char in[256];
  2.  
  3. void Text(const char *Buffer)
  4. {
  5. static char tempValue[256];
  6.  
  7. for(int i=0;i<=len;i++)
  8. {
  9.  
  10. sprintf(&tempValue[i],"%02x", (unsigned char)Buffer[i]);
  11. in[i]=(unsigned char)tempValue[i];
  12. cout<<"HexaValue"<<in[i];
  13.  
  14. }
  15.  
  16. }

this is not working as i wanted.
i want..

unsigned char in[] = {0x0044,ox0055,.....};

can anyone help me to correct this...
Last edited by Ancient Dragon; Apr 20th, 2008 at 11:49 am. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,436
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: 1473
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: convert char to hexadecimal

 
0
  #2
Apr 20th, 2008
you can try something like this: But your tempValue buffer may not be large enough, depending on the length of the string. Should be at least string length * 7, maybe larger.
  1. char temp[16];
  2. tempValue[0] = 0;
  3. // put below in a loop for each character
  4. sprintf(temp,"0x%04X, ", Buffer[i]);
  5. strcat(tempValue, temp);
Last edited by Ancient Dragon; Apr 20th, 2008 at 11:56 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
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



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC