# INT to Char

Status
Not open for further replies.

#### AtomSoft

##### Well-Known Member
Hey guys i have a number lets say 255 (0xFF)

I want to add 8 to it to make 0x0107(263) then divide it by lets say 16 to get approx 16 as the result (0x0010) now the problem is placing that new value into a char?

How can i place 0x0010 in to a char to make 0x10 ?

#### kchriste

##### New Member
Forum Supporter
int result = 16;
unsigned char destination;

destination = (unsigned char)result;

#### AtomSoft

##### Well-Known Member
Awesome!!! i had code somewhat like that but it was a pointer version heh

The * just had to come off .... Thanks a bunch

#### kchriste

##### New Member
Forum Supporter
Without the * it is called a CAST. It is not always needed in C, but it is one way to assure that the compiler does exactly what you want.

#### Mr RB

##### Well-Known Member
Since this is for a corrected integer rounded division you could do this;

Code:
unsigned char result;
if(blah > (255-8)) result  = 16;
else result = ((blah+8) / 16);

Which gives exactly the same output but only requires a char variable for result, saving one ram and a heap of ROm as it doesn't need any 16bit math.

#### AtomSoft

##### Well-Known Member
This is my current routine: converts 8 bit RED to 4 bit RED:

Code:
        temp = (((UINT)buff[5] + 8) /16);
if(temp == 16) temp-=1;
buff[5] = (BYTE)temp;

##### Banned
8 bit to 4 bit? Just divide it by 16.

Last edited:
Status
Not open for further replies.

Replies
5
Views
5K
Replies
13
Views
2K
Replies
10
Views
2K
Replies
6
Views
2K
Replies
2
Views
3K