[Icc-avr] Casting
Albert vanVeen
Albert.vanVeen at pertronic.co.nz
Tue Jan 15 11:11:08 PST 2008
I think there has been too much emphasis on the endians in this thread.
Nearly always these conversions are wanted for storage in byte-arrays or
comms buffers, and as long as storing and retrieving uses the same
method (and a few were mentioned here!) it doesn't matter where msb or
lsb is. Using Motorola (oops.. Freescale), TMS, PIC & AVR, I haven't
stumbled over it yet.
I hope this doesn't upset Ton's "There is no excuse for sloppy
programming." in which he is perfectly correct of course.
Albert.
-----Original Message-----
From: icc-avr-bounces at imagecraft.com
[mailto:icc-avr-bounces at imagecraft.com] On Behalf Of Johan H. Bodin
Sent: Tuesday, January 15, 2008 09:50 PM
To: Discussion list for ICCAVR and ICCtiny Users. You do NOT need
tosubscribe to icc-announce if you are a member of this.
Subject: Re: [Icc-avr] Casting
This works for me:
-----------
#ifndef BYTEPICK_INCLUDED
#define BYTEPICK_INCLUDED
/***********************************/
/* Johan Bodin SM6LKM 2007-06-01 */
/***********************************/
// This should ideally be predefined by the compiler...:
// ICCAVR7 uses little endian (LSByte first)
#define LITTLE_ENDIAN
//#define BIG_ENDIAN
// Sanity check:
#ifdef LITTLE_ENDIAN
#ifdef BIG_ENDIAN
#error "You can't have both endian types in bytepick.h!"
#endif
#endif
#ifndef LITTLE_ENDIAN
#ifndef BIG_ENDIAN
#error "You must select big/little endian in bytepick.h!"
#endif
#endif
// Here we go:
#ifdef BIG_ENDIAN
#define HIGH_BYTE_32(longvar) (*((unsigned char *)(&longvar)))
#define MID_H_BYTE_32(longvar) (*((unsigned char *)(&longvar) + 1))
#define MID_L_BYTE_32(longvar) (*((unsigned char *)(&longvar) + 2))
#define LOW_BYTE_32(longvar) (*((unsigned char *)(&longvar) + 3))
#define HIGH_BYTE_16(shortvar) (*((unsigned char *)(&shortvar)))
#define LOW_BYTE_16(shortvar) (*((unsigned char *)(&shortvar) + 1))
#else
#define HIGH_BYTE_32(longvar) (*((unsigned char *)(&longvar) + 3))
#define MID_H_BYTE_32(longvar) (*((unsigned char *)(&longvar) + 2))
#define MID_L_BYTE_32(longvar) (*((unsigned char *)(&longvar) + 1))
#define LOW_BYTE_32(longvar) (*((unsigned char *)(&longvar)))
#define HIGH_BYTE_16(shortvar) (*((unsigned char *)(&shortvar) + 1))
#define LOW_BYTE_16(shortvar) (*((unsigned char *)(&shortvar)))
#endif
#endif // BYTEPICK_INCLUDED
-----------
Regards
Johan Bodin
_______________________________________________
Icc-avr mailing list
Icc-avr at imagecraft.com
http://dragonsgate.net/mailman/listinfo/icc-avr
Scanned by Bizo Email Filter
More information about the Icc-avr
mailing list