Discussion Forum

onversion 8/16 bit types problem

Next Thread | Thread List | Previous Thread Start a Thread | Settings

DetailsMessage
Read-Only
Author
Sven Eibner
Posted
30-Jan-2001 07:49 GMT
Toolset
C166
New! onversion 8/16 bit types problem
Hi!

I've the following problem!
I receive via CAN a 16-Bit value, I'll use for some calculations on a
Siemens C167CR, und transmit the result again back via CAN!

The following code works fine on a PC:
(test 16 ist declared as an unsigned int, the byte_high and byte_low
as char)

test16 = byte_high;
test16 = test16 << 8;
test16 = test16 | byte_low;

The way back:

byte_low = test16;
byte_high = (test16 >> 8);

also works fine (on the PC)!

But the code doesn't work on the C167!
It seem's, that the C167 doesn't make the conversion back not correct.


Thanks for your tips!
SVEN
Read-Only
Author
Andrew Neil
Posted
30-Jan-2001 10:52 GMT
Toolset
C166
New! RE: onversion 8/16 bit types problem
(test 16 ist declared as an unsigned int, the byte_high and byte_low
as char)


Shouldn't the chars also be unsigned?
Otherwise, you're likely to run into sign-extension problems.

also works fine (on the PC)! But the code doesn't work on the C167!

Welcome to the world of platform-dependence!

What size is an unsigned int on your PC?

What exactly happens on your target? Have you tried stepping it in the simulator?

Read-Only
Author
Andrew Neil
Posted
30-Jan-2001 11:22 GMT
Toolset
C166
New! RE: onversion 8/16 bit types problem
See Mark Odell's site for examples of platform-independent data type definitions:
http://www.embeddedfw.com/
Read-Only
Author
Mark Odell
Posted
30-Jan-2001 14:47 GMT
Toolset
C166
New! RE: onversion 8/16 bit types problem
Remember, unsigned int on 32-bit OS's like Windows are 32-bits. For your 167, probably only 16. Be sure to always use unsigned vars. when bit shifting, including your chars. Try unsigned short on the PC to match the size of the 167's unsigned int.

Like Andrew Neil (thanks Andrew) said, see my web page's Coding Conventions document for known size typedefs.

- Mark

Next Thread | Thread List | Previous Thread Start a Thread | Settings