wrote:
> Hello,
>
> I need some help with the following:
>
> 1) I need to split a 16bit INT into two 8bit characters, and then be
> able to join these two characters to form again the original 16bit
> integer.
>
> For example with values expressed in binary:
>
> int n= 000000001111111;
> --> char a=00000000;
> --> char b=11111111;
>
> and then be able to join a and b to form back n.
>
You can use a mask for that.
a = n & 0xff00;
b = n & 0x00ff;
And use 'unsigned char' instead of 'char'. 'char' can be either
equivalent to 'unsigned char' or 'signed char'. If you rely on signed
char and use some value that cannot be represented with signed char,
then you may invoke implementation defined behavior or raise an
implementation defined signal.
>
> 2) And last, I need to convert an integer into a string.
> For example,
>
> int n=3.142 ---> char string[5] = ['3','.','1','4','2']
>
'int' is a part of the standard integer types and can't be used to
represent rationals. So you should either use an object type of float
or double. To produce a string from a given value of type float or
double, use sprintf/snprintf
>
> Thank you very much!! I've tried working with casting but as far as I
> know they don't seem to be the answer. I've also searched numerous
> tutorials and web pages, but nothing productive. Any help or
> suggestion will be highly appreciated. Thank you very much!!
casting is not a magical thing. The reason casting exists in the
language is for a few corner cases, namely: %p expecting a (void *)
with *printf family of functions. To compare the return value of mktime
to (time_t)-1 in case of error. For the is*, to* functions/macros.
There may be one or two other places that escape me at the moment but
not much else beyond that. The reason it doesn't produce a string is
because a cast yields a value of a given type, given a value. As there
does not exist a string type in C, then you can't expect that there
will be a string produced by a cast.
--
aegis