Re: [Sdcc-user] overflow in supposed to be unsigned long

2011-05-05 Thread Krishnendu Chatterjee
Hi, stdint.h is a standard C header file. If you use it in VC, you need to download it from net and place it in the include folder. If packing of struct is necessory, use "#pragma pack" compiler directive to resolve it. For example, #include #pragma pack(1) union U8x4_n_U32 { uint8_t u8[4

Re: [Sdcc-user] overflow in supposed to be unsigned long

2011-05-05 Thread Maarten Brock
Hi all, When portability is an issue my concerns would be endianness and packing which none of the posts have addressed. C does not guarantee much about the position of fields in a union or a struct or the position of bytes in a long int. Maarten > Am 05.05.2011 09:10, schrieb Krishnendu Chatter

Re: [Sdcc-user] overflow in supposed to be unsigned long

2011-05-05 Thread Philipp Klaus Krause
Am 05.05.2011 09:51, schrieb Krishnendu Chatterjee: > Hi, > > I don't understand what do you mean by "PORTABLE". Union is supported in > sdcc, gnu-arm, gcc and visual C. > And I have not seen any document stating that the usage of union in C is > bad :) > The problem is not the union. However ui

Re: [Sdcc-user] overflow in supposed to be unsigned long

2011-05-05 Thread Krishnendu Chatterjee
Hi, I don't understand what do you mean by "PORTABLE". Union is supported in sdcc, gnu-arm, gcc and visual C. And I have not seen any document stating that the usage of union in C is bad :) Krish On Thu, May 5, 2011 at 1:10 PM, Philipp Klaus Krause wrote: > Am 05.05.2011 09:10, schrieb Krishne

Re: [Sdcc-user] overflow in supposed to be unsigned long

2011-05-05 Thread Philipp Klaus Krause
Am 05.05.2011 09:10, schrieb Krishnendu Chatterjee: > Hi, > > My solution would be: > > #include > > union […] This seems to be the best so far, but none of the presented "solutions" from this thread is portable or good C. Here's one that is: unsigned char c[sizeof(unsigned long)]; unsigned l

Re: [Sdcc-user] overflow in supposed to be unsigned long

2011-05-05 Thread Krishnendu Chatterjee
Hi, My solution would be: #include 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 ** u

Re: [Sdcc-user] overflow in supposed to be unsigned long

2011-05-04 Thread Hynek Sladky
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, stof...@skulp.net napsal(a): Hi, I have to transmit an unsigned long as four bytes

Re: [Sdcc-user] overflow in supposed to be unsigned long

2011-05-04 Thread Erik Petrich
On Wed, 4 May 2011, Kevin Bailey wrote: On Wed, May 4, 2011 at 7:31 PM, Erik Petrich wrote:   long_counter = (unsigned int)(char_counter[0] | (char_counter[1] << 8)) |      ((unsigned long)(char_counter[2] | (char_counter[3] << 8)) << 16); I hope the first (unsigned int) cast isn't necessar

Re: [Sdcc-user] overflow in supposed to be unsigned long

2011-05-04 Thread Kevin Bailey
On Wed, May 4, 2011 at 7:31 PM, Erik Petrich wrote: > > >   long_counter = (unsigned int)(char_counter[0] | (char_counter[1] << 8)) | >      ((unsigned long)(char_counter[2] | (char_counter[3] << 8)) << 16); I hope the first (unsigned int) cast isn't necessary, because the char_counter[1] << 8 wo

Re: [Sdcc-user] overflow in supposed to be unsigned long

2011-05-04 Thread Erik Petrich
On Thu, 5 May 2011, Kristoffer Ek wrote: > > On 4.5.2011, at 6:20, Erik Petrich wrote: > >> long_counter = (char_counter[0] | (char_counter[1] << 8)) | >> ((unsigned long)(char_counter[2] | (char_counter[3] << 8)) << 16); > > > This overflows at 32767 :-/ > > -- stoffer Sorry, I didn't ta

Re: [Sdcc-user] overflow in supposed to be unsigned long

2011-05-04 Thread Kristoffer Ek
On 4.5.2011, at 6:20, Erik Petrich wrote: > long_counter = (char_counter[0] | (char_counter[1] << 8)) | > ((unsigned long)(char_counter[2] | (char_counter[3] << 8)) << 16); This overflows at 32767 :-/ -- stoffer ---

Re: [Sdcc-user] overflow in supposed to be unsigned long

2011-05-03 Thread Erik Petrich
On Wed, 4 May 2011, stof...@skulp.net wrote: > 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? > // split long_co

[Sdcc-user] overflow in supposed to be unsigned long

2011-05-03 Thread stoffer
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?