How to convert from integer to unsigned char in C, given integers larger than 256?

Posted by Alf_InPogform on Stack Overflow See other posts from Stack Overflow or by Alf_InPogform
Published on 2011-01-07T20:20:16Z Indexed on 2011/01/07 20:53 UTC
Read the original article Hit count: 214

Filed under:
|
|
|
|

As part of my CS course I've been given some functions to use. One of these functions takes a pointer to unsigned chars to write some data to a file (I have to use this function, so I can't just make my own purpose built function that works differently BTW). I need to write an array of integers whose values can be up to 4095 using this function (that only takes unsigned chars).

However am I right in thinking that an unsigned char can only have a max value of 256 because it is 1 byte long? I therefore need to use 4 unsigned chars for every integer? But casting doesn't seem to work with larger values for the integer. Does anyone have any idea how best to convert an array of integers to unsigned chars?

© Stack Overflow or respective owner

Related posts about c

    Related posts about casting