Hi,

My solution would be:

#include <stdint.h>

union U8x4_n_U32
{
    uint8_t u8[4];
    uint32_t u32;
};

union U8x4_n_U32 u8x4_n_u32;

// uint32_t to array of uint8_t conversion
u8x4_n_u32.u32 = 0xA0459201;
/**********************************************
** Now you can access the individual bytes as
** u8x4_n_u32.u8[0]
** u8x4_n_u32.u8[1]
** u8x4_n_u32.u8[2]
** u8x4_n_u32.u8[3]
**********************************************/

AND

// Array of uint8_t to uint32_t conversion
u8x4_n_u32.u8[0] = 0x01;
u8x4_n_u32.u8[1] = 0x92;
u8x4_n_u32.u8[2] = 0x45;
u8x4_n_u32.u8[3] = 0xA0;
/**********************************************************
** Now you can access all the bytes as unsigned long using
** u8x4_n_u32.u32
***********************************************************/

Krish

On Thu, May 5, 2011 at 10:48 AM, Hynek Sladky <econf@centrum.cz> wrote:
Hi,

I would use something like:


for (i=0; i<4; i++)
  char_counter[i] = ((unsigned char*)&long_counter)[i];

for (i=0; i<4; i++)
  ((unsigned char*)&long_counter)[i] =
char_counter[i];

Hynek



Dne 4.5.2011 4:08, stoffer@skulp.net napsal(a):
Hi, I have to transmit an unsigned long as four bytes, so I split them up
and join them again, but with the folowing example code, I get overflow at
long_counter>32767 - can any of you give me a clue how to do it right?

<http://code.google.com/p/kodak-timer/source/browse/ir_test/four_unsigned_char_to_unsigned_long.c>

unsigned char i;
unsigned long long_counter;
unsigned char char_counter[4];

void main(void) {
	unsigned char len;
	unsigned char lcd_buf[16];

	OSCCONbits.SCS = 0x0;		// System Clock Select bits = External oscillator
	OSCCONbits.IRCF = 0x7;		// Internal Oscillator Frequency Select bits 8
MHz (INTOSC drives clock directly)

	lcd_init();
	while (1) {
		for (long_counter = 0; long_counter < 0xffff0000; long_counter += 50) {
			// split long_counter to four unsigned char's
			for (i = 0; i < 4; i++) {
				char_counter[i] = (long_counter & (0xff << (i * 8))) >> (i * 8);
			}

			// convert them to unsigned int again
			long_counter = char_counter[0] | (char_counter[1] << 8) |
(char_counter[2] << 16) | (char_counter[3] << 24);
			sprintf(lcd_buf, "%lu", long_counter);
			debug(lcd_buf);
		}
	}
}

P.S. Compiling for pic18f2550

- kristoffer ek

------------------------------------------------------------------------------
WhatsUp Gold - Download Free Network Management Software
The most intuitive, comprehensive, and cost-effective network
management toolset available today.  Delivers lowest initial
acquisition cost and overall TCO of any competing solution.
http://p.sf.net/sfu/whatsupgold-sd
_______________________________________________
Sdcc-user mailing list
Sdcc-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sdcc-user