View previous topic :: View next topic |
Author |
Message |
JuGeR Guest
|
Hexadecimal as INT8 to char (Sprintf problem) |
Posted: Tue Aug 11, 2009 12:41 pm |
|
|
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
|
|
Posted: Tue Aug 11, 2009 12:51 pm |
|
|
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
|
|
Posted: Tue Aug 11, 2009 1:33 pm |
|
|
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
|
|
Posted: Tue Aug 11, 2009 1:42 pm |
|
|
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. |
|
|
|