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

Hexadecimal as INT8 to char (Sprintf problem)

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







Hexadecimal as INT8 to char (Sprintf problem)
PostPosted: Tue Aug 11, 2009 12:41 pm     Reply with quote

Dear all,

First of all, thank you for using your time to answer me.

We have the variable:
Code:
int num=0x13f2;  // hexadecimal

I want it to be in a char so that :
Code:
char res[20];

res[0]=1
res[1]=3
res[2]=f
res[3]=2

I have tried with sprintf, so that:
Code:

int tot=0;

tot=sprintf(res, "%x", num);

I am getting tot=2 as a result and only the last 2 numbers are being stored.

Could you please help me with this issue?

Thank you very much in advance.
Best regards,
Javi
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Aug 11, 2009 12:51 pm     Reply with quote

Quote:

int num=0x13f2; // hexadecimal

int tot=0;

tot=sprintf(res, "%x", num);

You need to learn about CCS data types. They're different. In CCS,
an 'int' is an 8-bit unsigned integer. In CCS, an unsigned 16-bit integer
is a 'long' or also 'int16'.

You also need to learn about format strings for printf and sprintf in CCS.
Again, they're different than in "normal" C. To display an integer larger
than a byte, you need to use "%lx". That's an 'L' in front of the 'x'.

These things are discussed in the CCS manual, in the "Basic Types"
section and in the "printf" section.
http://www.ccsinfo.com/downloads/ccs_c_manual.pdf
Guest








PostPosted: Tue Aug 11, 2009 1:33 pm     Reply with quote

Dear PCM,

I just have to tell you 3 words:

THANKS A LOT!

It works perfectly now. I will follow your advice and read carefully the manual.

Thanks again.
Best regards,

Javi
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Tue Aug 11, 2009 1:42 pm     Reply with quote

CCS C is older than ANSI C and it conforms closer to the original K&R language specification than to ANSI. For example the original K&R says an int should be the natural data width of the processor, which for the older PICs is 8 bits. ANSI came along later and said an int should always be 16 bits regardless of the hardware, which on narrow chips makes processing an int slow.
_________________
The search for better is endless. Instead simply find very good and get the job done.
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