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 support@ccsinfo.com

inttoascii or hextoascii function?

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







inttoascii or hextoascii function?
PostPosted: Mon Dec 16, 2002 9:17 am     Reply with quote

Does anyone know where I can find an inttoascii or hextoascii function?
___________________________
This message was ported from CCS's old forum
Original Post ID: 10065
R.J.Hamlett
Guest







Re: inttoascii or hextoascii function?
PostPosted: Mon Dec 16, 2002 9:43 am     Reply with quote

:=Does anyone know where I can find an inttoascii or hextoascii function?

sprintf

For your requirements:

char output[5];

sprintf(output,"\%D",value_to_convert);

Will put into the string 'output', the ASCII equivalent of the number in 'value_to_convert'. Using '\%ld', instead of '\%D', will allow a long integer to be sent. With this the output could be seven digits 'wide' ('-' sign, 5 digits, and the terminating '\0'), so the array would need to be larger.

Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 10066
Dave Yeatman
Guest







Re: inttoascii or hextoascii function?
PostPosted: Mon Dec 16, 2002 4:07 pm     Reply with quote

Depending on what you are doing, converting individual numbers is easy. This would be the building block for whatever you want to do.

If a decimal digit simply add 48 to make it the ascii equivalent.

For hex:
If <=9 then add 0x30, else add 0x37.

I created a function that I call with the digit to convert and it returns a char. I also pass a boolean flag "hex" to tell whether to convert hex or decimal. Once created, the routine works everywhere and you can paste it into any code.

Example protoype:
char convert(byte digit, boolean hex)

Good Luck!

Dave
___________________________
This message was ported from CCS's old forum
Original Post ID: 10078
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