# decimal to hex and hex to decimal conversion in ccs pic compiler.

Status
Not open for further replies.

#### Noman Yousaf

##### Full Member level 4
hi
i simply need to convert decimal (8 bit) to hex in CCS. In mikroC, that is so easy to convert by using "bcd2dec" built in function. but in CCS i think there is no any function like that so how can i do?

#### FvM

##### Super Moderator
Staff member
CCS itoa() and atoi() can work with hex.

#### Easyrider83

Code:
unsigned char DecToBCD (unsigned char Value)
{
return ((Value / 10) << 4) | (Value % 10);
}

unsigned char BcdToDec (unsigned char Value)
{
return ((Value >> 4) * 10) + (Value & 0x0F);
}

Points: 2

### Noman Yousaf

Points: 2

#### FvM

##### Super Moderator
Staff member
The code is right for BCD conversion and should be equivalent to the respective MikroC built-in functions. Hex conversion as asked in the question title is a different thing.

#### Easyrider83

How can I help if I can't understand what does he means?
HEX to string conversion?
Code:
void ValueToStringHEX_Byte (unsigned char Value, char * String)
{
const unsigned char HEX_Var[17]={"0123456789ABCDEF"};
* String++ =   ((Value&0xF0)>>4)[HEX_Var];
* String   =    (Value&0x0F)[HEX_Var];
}

Status
Not open for further replies.