sprintf x signed values

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

sprintf x signed values

2,673 次查看
Ricardo_RauppV
Contributor I
Hi people
I´m working with 52235 and I´m trying to print a negative value for a char (int8) wich is -2, by using sprintf function, but the output is "254".  
I did:
{
 
int8 sdata_8;
 
   sdata_8=-2;
   sprintf(lcd_controller.text_data_array,"   %hi.%01u",
           sdata_816/10,
 
I did the same experience using int16 and so I had success..
Is there some special care to use sprintf with char?
 
By the way, how can copy a negative value of a int8 to a int16?
I tryied:
sdata_16=sdata_8; and I got the calue 254 ..not -02 ...
 
Thanks guys ...
Ricardo Raupp
 
标签 (1)
标记 (1)
0 项奖励
回复
3 回复数

1,144 次查看
CompilerGuru
NXP Employee
NXP Employee
Is this int8 type you are using signed?
It apears to be unsigned, note that
typedef signed char int8;
is needed, a typedef "typedef char int8;" only creates a singed type if char is signed, and that is defined by the compiler, or the ABI the compiler follows.

Daniel
0 项奖励
回复

1,144 次查看
Ricardo_RauppV
Contributor I
Daniel
 
You were rigth...my char was (some spirit did it..I don´t remember...) defined as unsigned char by default.
Follow attached the CW screeshoot where it was set...
After I reset it...I got happy again ...
Now it is working fine...
Saturday nigth will ne good ....
Thanks for your attention!!!!!!
Ricardo Raupp
0 项奖励
回复

1,144 次查看
Ricardo_RauppV
Contributor I
Dear Daniel
I think the int8 definition  is ok.
I extract the below form ...\include\mcf52xxx.h
 
/*
 * The basic data types
 */
typedef unsigned char       uint8;  /*  8 bits */
typedef unsigned short int  uint16; /* 16 bits */
typedef unsigned long int   uint32; /* 32 bits */
typedef char                int8;   /*  8 bits */
typedef short int           int16;  /* 16 bits */
typedef int                 int32;  /* 32 bits */
typedef volatile int8       vint8;  /*  8 bits */
typedef volatile int16      vint16; /* 16 bits */
typedef volatile int32      vint32; /* 32 bits */
typedef volatile uint8      vuint8;  /*  8 bits */
typedef volatile uint16     vuint16; /* 16 bits */
typedef volatile uint32     vuint32; /* 32 bits */
 
Curious:
- In a text:
a=-2;
 if(a>0.) ...
The answer was true !!!like the considered value was 254 and not -2 ...
Should I change int8 definition?

Thanks for "saturday help" !!!!

Ricardo Raupp

 
0 项奖励
回复