data type mistake

bridged with qdn.public.devtools
Post Reply
Yijun Zou

data type mistake

Post by Yijun Zou » Tue Oct 21, 2003 9:53 am

Hi,

I'm now working on a serial communication program. Write on ser3 and receive
the data on ser4. ser3 and ser4 are directly wired. Basically, it works
well. Only a little annoying problem is the first byte I received is of
wrong type. As follows:

In my code,
char data[22]; //to contain the received data from ser4

to display the result, I use:
printf("the first byte is 0x%x.\n",data[0]);

The first byte should be 0xb8. But when the program runs, the result is very
weird. the data[0]=0xffffffb8.
Similarly, the data[1]=0xffffffa8. However, from the data[2] on, the data
are of correct type. For example, data[2]=ox4;...

Can anybody explain this problem? What's wrong with it?

Mario Charest

Re: data type mistake

Post by Mario Charest » Tue Oct 21, 2003 1:17 pm

"Marian Oklapek" <oklapek@pobox.sk> wrote in message
news:3F953A82.1050908@pobox.sk...
Hello Yijun,

change yours type of data to unsigned char data[22];
You need this because 0xb8 when store in a char is a negative number. When
the char is passed to the printf it`s automaticaly promoted to an integer
and thus subject to sign extension thus 0xb8 becomes 0xffffffb8.
Best regards
Marian.

Yijun Zou wrote:
Hi,

I'm now working on a serial communication program. Write on ser3 and
receive
the data on ser4. ser3 and ser4 are directly wired. Basically, it works
well. Only a little annoying problem is the first byte I received is of
wrong type. As follows:

In my code,
char data[22]; //to contain the received data from ser4

to display the result, I use:
printf("the first byte is 0x%x.\n",data[0]);

The first byte should be 0xb8. But when the program runs, the result is
very
weird. the data[0]=0xffffffb8.
Similarly, the data[1]=0xffffffa8. However, from the data[2] on, the
data
are of correct type. For example, data[2]=ox4;...

Can anybody explain this problem? What's wrong with it?






Marian Oklapek

Re: data type mistake

Post by Marian Oklapek » Tue Oct 21, 2003 1:54 pm

Hello Yijun,

change yours type of data to unsigned char data[22];

Best regards
Marian.

Yijun Zou wrote:
Hi,

I'm now working on a serial communication program. Write on ser3 and receive
the data on ser4. ser3 and ser4 are directly wired. Basically, it works
well. Only a little annoying problem is the first byte I received is of
wrong type. As follows:

In my code,
char data[22]; //to contain the received data from ser4

to display the result, I use:
printf("the first byte is 0x%x.\n",data[0]);

The first byte should be 0xb8. But when the program runs, the result is very
weird. the data[0]=0xffffffb8.
Similarly, the data[1]=0xffffffa8. However, from the data[2] on, the data
are of correct type. For example, data[2]=ox4;...

Can anybody explain this problem? What's wrong with it?




Yijun Zou

Re: data type mistake

Post by Yijun Zou » Wed Oct 22, 2003 2:11 am

Hi Mario and Marian,

I do as you advised and it's OK now. :D
Thank you both!

Yijun

"Mario Charest" <postmaster@127.0.0.1> дÈëÏûÏ¢ÐÂÎÅ
:bn3amm$d56$1@inn.qnx.com...
"Marian Oklapek" <oklapek@pobox.sk> wrote in message
news:3F953A82.1050908@pobox.sk...
Hello Yijun,

change yours type of data to unsigned char data[22];

You need this because 0xb8 when store in a char is a negative number.
When
the char is passed to the printf it`s automaticaly promoted to an integer
and thus subject to sign extension thus 0xb8 becomes 0xffffffb8.


Best regards
Marian.

Yijun Zou wrote:
Hi,

I'm now working on a serial communication program. Write on ser3 and
receive
the data on ser4. ser3 and ser4 are directly wired. Basically, it
works
well. Only a little annoying problem is the first byte I received is
of
wrong type. As follows:

In my code,
char data[22]; //to contain the received data from ser4

to display the result, I use:
printf("the first byte is 0x%x.\n",data[0]);

The first byte should be 0xb8. But when the program runs, the result
is
very
weird. the data[0]=0xffffffb8.
Similarly, the data[1]=0xffffffa8. However, from the data[2] on, the
data
are of correct type. For example, data[2]=ox4;...

Can anybody explain this problem? What's wrong with it?








Post Reply

Return to “qdn.public.devtools”