Link to home
Start Free TrialLog in
Avatar of naseeam
naseeamFlag for United States of America

asked on

Can function argument type char be typecasted to unsigned int?

Please consider following function definition:

/* write a new value into a dataword */
unsigned int replace_byte (unsigned int dataword, int bytenum,
  char byteval)


Can I typecast byteval as follows:
(unsigned int)byteval                    
         
If yes, what will be three most significant bytes of byteval after the typecasting.  Will they be zeros.
ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
To avoid that, use an unsigned char :

        unsigned int fun(unsigned char byteval) {
            return (unsigned int)byteval;
        }
>> To avoid that, use an unsigned char :

In that case, the 3 most significant bytes of the unsigned int will ALWAYS be all 0's.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
NB. By MSBs I mean Most Significant Bytes  :)