![]() |
| | #1 |
| Registered User Join Date: Nov 2008
Posts: 8
| Help with cast in C! I wanna cast 32bit integer into 8bit char value, and the problem is that I don't know which 8bits of the integer would be taken and stored in the 8bit char variable?! The most significant or least significant?!! |
| Tirania is offline | |
| | #2 |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| If you use a plain cast, then the lowest 8 bits are being used. This is probably the answer you wanted. Slightly more complex situation: If you cast a pointer to another pointer type, and then dereference it, then it's the 8 bits with the lowest address you get. Depending on the byte-order of the machine, you get EITHER the lowest or the highest. -- Mats
__________________ Compilers can produce warnings - make the compiler programmers happy: Use them! Please don't PM me for help - and no, I don't do help over instant messengers. |
| matsp is offline | |
| | #3 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,365
| Quote:
Frankly, it would be clearer to use bitwise operations to extract those bits that you want.
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |
| laserlight is online now | |
| | #4 | |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Quote:
-- Mats
__________________ Compilers can produce warnings - make the compiler programmers happy: Use them! Please don't PM me for help - and no, I don't do help over instant messengers. | |
| matsp is offline | |
| | #5 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,365
| Quote:
Code: char c = (char)(i & 0xff); // where i is the source integer
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |
| laserlight is online now | |
![]() |
| Tags |
| c code, cast, char, integer |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Including The Right DLLs | bumfluff | Game Programming | 8 | 12-28-2006 03:32 AM |
| How to fix misaligned assignment statements in the source code? | biggyK | C++ Programming | 28 | 07-16-2006 11:35 PM |
| Converting Double to Float | thetinman | C++ Programming | 7 | 06-17-2006 02:46 PM |
| errors in class(urgent ) | ayesha | C++ Programming | 1 | 11-10-2001 10:14 PM |
| errors in class(urgent) | ayesha | C++ Programming | 2 | 11-10-2001 06:51 PM |