CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Transform string with Character to string with Hex

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
karlosguay



Joined: 12 Jun 2013
Posts: 20

View user's profile Send private message

Transform string with Character to string with Hex
PostPosted: Wed Dec 17, 2014 6:58 am     Reply with quote

Hi, i have a string with ascii characters:

MyString="HELLO"

and i need transform this string in another string with Hex values without 0x.

Result must be

MyHexString="48454C4C4F"

How i can do it??

Thanks
Ttelmah



Joined: 11 Mar 2010
Posts: 19537

View user's profile Send private message

PostPosted: Wed Dec 17, 2014 8:10 am     Reply with quote

Assuming string is called 'source':

Code:

   int8 ctr=0;
   char destination[15] = {""};
   char temp[3];

   while (source[ctr]!='\0')
   {
      sprintf(temp,"%02X",source[ctr++]);
      strcat(destination,temp);
   }


Various things. The initialisation of 'destination' is needed so it contains a null terminator or strcat won't work. Within the limits of the size of 'destination', this handles any length string, till it sees the null terminator.
It just converts character by character, and builds the destination up.
The %02X format, forces every character to hex in the form XX two digits each time, with leading zeros.
Further comments, the 0x won't be put in the output, and the capital 'X' is needed to give the capital return you want. I made destination an odd size since it will hold two characters for each character in the input string, and a null terminator.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group