Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

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 ?
 
int result = 16;
unsigned char destination;

destination = (unsigned char)result;
 
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.
 
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.
 
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;
 
8 bit to 4 bit? Just divide it by 16.
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top