# How to convert to hex?

Status
Not open for further replies.

#### internetuser2k12

##### Banned
Hello!

I have a char variable mydec which holds a value 153. I want to know how to convert this 153 decimal to 0x99 and store it in another char variable like myhex.

The stored value should be in the form 0x99, i.e., 0 and x has to be concatanated to 99 and stored in myhex variable.

Is there any vay to concatanate 0 and x and 99 in C language? I am using mikroC Pro.

The upper nibble of mydec holds 9 and the lower nibble holds 9 as well.
First get the upper nibble and then get the lower nibble.
Next just add '0' and 'x' to the retrieved data.

Declare myhex as char. myhex = '0', myhex='x', myhex=value of upper nibble, myhex=value of lower nibble.

You can easily get value of upper nibble and lower nibble by bit-shifting.

Hope this helps.
Tahmid.

Last edited:
• Roger Miller

### Roger Miller

Points: 2
OK Thank you Tahmid.

OK Thank you Tahmid.

This is not what you want, you keep asking wrong things and confuse people.
What you wan want is to convert a decimal to BCD and you keep creating threads all over the forum asking the same things over and over again.
Are you missing basic C knowledge?

The only thing that a microcontroler understands is binary, every other numeral system we use is just for better understanding or convenience for the humans.

The same value can be represented in several numeral systems

Code:
unsigned char my_char;

my_char= 153;    // decimal system
my_char= 0b10011001;    // binary system

In all three cases the values assigned to the variable are exactly the same

A decimal value 0f 99 when converted to BCD equals hexadecimal 0x99 or decimal 153 or binary 0x10011001.

I want to know how to convert this 153 decimal to 0x99 and store it in another char variable like myhex.
There is no need for a conversion, these two represent the same number is two different numeric systems.

• FvM

### FvM

Points: 2
But what if I want to give bcd 0x99 i.e., decimal 153 as 0x99 to the I2C1_Wr() function? It takes values like I2C1_Wr(0x99) and I know I can also write it as I2C1_Wr(153), but I want to write it as 0x99. so how to put 0 and x and 99 in one unsigned char variable?

Was you collage course related to programming?
If so I don't get it, you may want to consider going back and repeat the courses ...8-O

But what if I want to give bcd 0x99 i.e., decimal 153 as 0x99 to the I2C1_Wr() function? It takes values like I2C1_Wr(0x99) and I know I can also write it as I2C1_Wr(153), but I want to write it as 0x99. so how to put 0 and x and 99 in one unsigned char variable?

Writing this:
Code:
I2C1_Wr(0x99)

is the same as writing:
Code:
I2C1_Wr(153)

as is writing:
Code:
0b10011001;

I know that, but in code I want to set the variable myhec to something like 0x99. I have decimal 99 in mydec variable. I have to concatanate 0 and x and mydec and put it in myhex.

- - - Updated - - -

@ alexan_e

I don't have to repeat the courses. I just want to use 0x99 instead of d153 in my code.

If myhex is a string or character array and you want to store "0x99" in it, use the idea I presented in post # 2: https://www.edaboard.com/threads/272779/#post1169785

- - - Updated - - -

@ alexan_e

I don't have to repeat the courses. I just want to use 0x99 instead of d153 in my code.

So, go ahead and write 0x99 instead. Since 0x99 is the same thing as d153, there won't be any problem.

I don't have to repeat the courses. I just want to use 0x99 instead of d153 in my code.

The only way this would make sense is if you were asking for an ASCII representation of a decimal value in hexadecimal format but this has already been explained https://www.edaboard.com/threads/272756/#post1169574 and was not what you wanted, is it now?

can I make a char array and store it like this

myhex = 0;
myhex = 'x'
myhex = 99

and then use I2C1_Wr(myHex)?

- - - Updated - - -

@alexan_e It's just that I want to represent numbers in hexadecimal. I get 0x99 for d153. but actually dec2bcd(153) gives it as 99 and not 0x99. So I want to put that 99 as 0x99 in I2C1_Wr(myhex) function.

Write this:

Code:
I2C1_Wr(0x99);

No conversions needed.

It's the same as:

Code:
I2C1_Wr(153);

Hexadecimal, decimal, binary and octal are different ways of representing the data. 153 in decimal has a corresponding value for hexadecimal, one for binary and one for octal. However, they store the same value. So, writing 0x99 instead of 153 is the same thing. It assigns the same value.

can I make a char array and store it like this

myhex = 0;
myhex = 'x'
myhex = 99

and then use I2C1_Wr(myHex)?

I2C1_Wr() expects an byte variable if I'm not mistaken so how do you expect this to work by sending a pointer?

Last edited:

can I make a char array and store it like this

myhex = 0;
myhex = 'x'
myhex = 99

and then use I2C1_Wr(myHex)?
No. The question raises doubts about your understanding of microprocessor operation. The argument of I2C1_Wr() has to be a single 8-bit number.

To convert a hexadecimal string to a numeric variable, you need to use scanf() or a similar compiler specific function. Also atoi might accept hexadecimal strings.

We know however, that you have a numeric value of 99 originally. And we also know, that dec2bcd() provides a perfect way to achieve what you want.

• tpetar

### tpetar

Points: 2
No. I dont have the valur 0x99. I have d153. I have to convert it into hex and assign it to I2C1_Wr() function myhex should store 0x99.
If I use DectoBcd(153) I get 99 as result and not 0x99. so how to make that 99 to 0x99?

I dont have the valur 0x99. I have d153
I guess, it has been said a hundred times that both are the same.

P.S.:
If I use DectoBcd(153) I get 99 as result and not 0x99. so how to make that 99 to 0x99?
Now you are mixing-up everything. I see strong indications that you need some sleep.

• bigdogguru and tpetar

Points: 2

### bigdogguru

Points: 2
my values are varying fro 0 to 153 decimal. see the switch case statement for inc and dec buttons in the following code.

Code:
#ifndef DS1307
#define DS1307 0xD0
#endif

#define SET RA0_bit
#define INC RA1_bit
#define DEC RA2_bit
#define OK RA3_bit

// LCD module connections
sbit LCD_RS at RB2_bit;
sbit LCD_EN at RB3_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;

sbit LCD_RS_Direction at TRISB2_bit;
sbit LCD_EN_Direction at TRISB3_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// End LCD module connections

unsigned char num, flag, set_time_flag;
char b, x, i, sec, min1, hr, week_day, day, mn, year;
char *txt, tnum, time_format, sihr;
char colon[] = ":";
int cnt, ihr, error, am_pm = 0;
bit set_flag, inc_flag, dec_flag, ok_flag, do_once_hr, do_once_min, do_once_sec, do_once_day, do_once_mn, do_once_year;
bit dec_do_once_hr, dec_do_once_min, dec_do_once_sec, dec_do_once_day, dec_do_once_mn, dec_do_once_year;
bit inc_do_once_hr, inc_do_once_min, inc_do_once_sec, inc_do_once_day, inc_do_once_mn, inc_do_once_year;
bit alarm_flag, alarm_set_flag;
unsigned char set_cnt, inc_cnt, dec_cnt = 0;
unsigned char ssec, smin, shr, sweek_day, sday, smn, syear;
unsigned char s_sec, s_min, s_hr, s_week_day, s_day, s_mn, s_year;
unsigned char asec, amin, ahr, aweek_day, aday, amn, ayear;

void Zero_Fill(char *value) {
if (value == 0) {
value = value;
value = 48;
value = 0;
}
}

void Write_Time() {
I2C1_Start();          // issue start signal
I2C1_Wr(0);            // start from word at address (REG0)
I2C1_Wr(0xD7);         // write \$80 to REG0. (pause counter + 0 sec)
I2C1_Wr(s_min);         // write 11 to minutes word to (REG1)
I2C1_Wr(s_hr);          // write 17 to hours word (24-hours mode)(REG2)
I2C1_Wr(s_week_day);    // write 6 - Friday (REG3)
I2C1_Wr(s_day);         // write 23 to date word (REG4)
I2C1_Wr(s_mn);          // write 11 (Nov) to month word (REG5)
I2C1_Wr(s_year);        // write 12 to year word (REG6)
I2C1_Stop();           // issue stop signal

I2C1_Start();          // issue start signal
I2C1_Wr(0);            // start from word at address 0
I2C1_Wr(0);            // write 0 to REG0 (enable counting + 0 sec)
I2C1_Stop();           // issue stop signal
}

void Read_Time(char *sec, char *min, char *hr, char *week_day, char *day, char *mn, char *year) {
I2C1_Start();
I2C1_Wr(DS1307);
I2C1_Wr(0);
I2C1_Repeated_Start();
I2C1_Wr(0xD1);
*sec = I2C1_Rd(1);
*min = I2C1_Rd(1);
*hr = I2C1_Rd(1);
*week_day = I2C1_Rd(1);
*day = I2C1_Rd(1);
*mn = I2C1_Rd(1);
*year = I2C1_Rd(0);
I2C1_Stop();

I2C1_Start();
I2C1_Wr(DS1307);
I2C1_Wr(0);
I2C1_Repeated_Start();
I2C1_Wr(0xD1);
s_sec = I2C1_Rd(1);
s_min = I2C1_Rd(1);
s_hr = I2C1_Rd(1);
s_week_day = I2C1_Rd(1);
s_day = I2C1_Rd(1);
s_mn = I2C1_Rd(1);
s_year =I2C1_Rd(0);
I2C1_Stop();

ssec = s_sec;
smin = s_min;
shr = s_hr;
sweek_day = s_week_day;
sday = s_day;
smn = s_mn;
syear = s_year;

}

void Transform_Time(char  *sec, char *min, char *hr, char *week_day, char *day, char *mn, char *year) {
*sec  =  ((*sec & 0x70) >> 4)*10 + (*sec & 0x0F);
*min  =  ((*min & 0xF0) >> 4)*10 + (*min & 0x0F);
*hr   =  ((*hr & 0x30) >> 4)*10 + (*hr & 0x0F);
*week_day =(*week_day & 0x07);
*day  =  ((*day & 0xF0) >> 4)*10 + (*day & 0x0F);
*mn   =  ((*mn & 0x10) >> 4)*10 + (*mn & 0x0F);
*year =  ((*year & 0xF0)>>4)*10+(*year & 0x0F);
/*
ssec  =  ((*sec & 0x70) >> 4)*10 + (*sec & 0x0F);
smin  =  ((*min & 0xF0) >> 4)*10 + (*min & 0x0F);
shr   =  ((*hr & 0x30) >> 4)*10 + (*hr & 0x0F);
sweek_day =(*week_day & 0x07);
sday  =  ((*day & 0xF0) >> 4)*10 + (*day & 0x0F);
smn   =  ((*mn & 0x10) >> 4)*10 + (*mn & 0x0F);
syear =  ((*year & 0xF0)>>4)*10+(*year & 0x0F);
*/
/*
ssec = ((ssec >> 4)*10+(ssec & 0x0F));
smin = ((smin >> 4)*10+(smin & 0x0F));
shr = ((shr >> 4)*10+(shr & 0x0F));
sweek_day = ((sweek_day >> 4)*10+(s_week_day & 0x0F));
sday = ((sday >> 4)*10+(sday & 0x0F));
smn = ((smn >> 4)*10+(smn & 0x0F));
syear = ((syear >> 4)*10+(syear & 0x0F));
*/
}

void Display_Time(char sec, char min, char hr, char week_day, char day, char mn, char year) {
if(alarm_flag == 0) {
switch(week_day){
case 1: txt="Sun"; break;
case 2: txt="Mon"; break;
case 3: txt="Tue"; break;
case 4: txt="Wed"; break;
case 5: txt="Thu"; break;
case 6: txt="Fri"; break;
case 7: txt="Sat"; break;
}
Lcd_Out(1,1,txt);
Lcd_Chr(1,5,(day / 10)   + 48);    // Print tens digit of day variable
Lcd_Chr(1,6, (day % 10)   + 48);    // Print oness digit of day variable
Lcd_Chr(1,7,'/');
Lcd_Chr(1,8,(mn / 10) + 48);
Lcd_Chr(1,9,(mn % 10) + 48);
Lcd_Chr(1,10,'/');
Lcd_Out(1,11,"2");
Lcd_Out(1,12,"0");
Lcd_Chr(1,13, (year / 10)  + 48);          // Print year vaiable + 8 (start from year 2008)
Lcd_Chr(1,14, (year % 10)  + 48);

/*
if(hr >= 12) {
Lcd_Out(2,2,"PM");
am_pm = 1;
}
else if(hr < 12) {
Lcd_Out(2,2,"AM");
am_pm = 0;
}
*/

Lcd_Chr(2,5,(hr / 10)  + 48);
Lcd_Chr(2,6,(hr % 10)  + 48);
Lcd_Out(2,7,":");
Lcd_Chr(2,8,(min / 10) + 48);
Lcd_Chr(2,9,(min % 10) + 48);
Lcd_Out(2,10,":");
Lcd_Chr(2,11,(sec / 10) + 48);
Lcd_Chr(2,12,(sec % 10) + 48);
}
}

void Display_Alarm_Time(char asec, char amin, char ahr, char aweek_day, char aday, char amn, char ayear) {
if(alarm_flag == 1) {
Lcd_Out(1,1,"ALR");
Lcd_Chr(1,5,(aday / 10)   + 48);    // Print tens digit of day variable
Lcd_Chr(1,6, (aday % 10)   + 48);    // Print oness digit of day variable
Lcd_Chr(1,7,'/');
Lcd_Chr(1,8,(amn / 10) + 48);
Lcd_Chr(1,9,(amn % 10) + 48);
Lcd_Chr(1,10,'/');
Lcd_Out(1,11,"2");
Lcd_Out(1,12,"0");
Lcd_Chr(1,13, (ayear / 10)  + 48);          // Print year vaiable + 8 (start from year 2008)
Lcd_Chr(1,14, (ayear % 10)  + 48);
Lcd_Chr(2,5,(ahr / 10)  + 48);
Lcd_Chr(2,6,(ahr % 10)  + 48);
Lcd_Out(2,7,":");
Lcd_Chr(2,8,(amin / 10) + 48);
Lcd_Chr(2,9,(amin % 10) + 48);
Lcd_Out(2,10,":");
Lcd_Chr(2,11,(asec / 10) + 48);
Lcd_Chr(2,12,(asec % 10) + 48);
}
}

void main() {

TRISA = 0b00001111;
PORTA = 0b00000000;
TRISB = 0b00000000;
PORTB = 0b00000000;

Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1,4,"RTC DS1307");
Delay_ms(3000);
I2C1_Init(100000);
Lcd_Cmd(_LCD_CLEAR);

set_time_flag = 0;
ok_flag = 1;
alarm_flag = 0;
alarm_set_flag = 0;

ssec = 0x00;
smin = 0x00;
shr = 0x00;
sweek_day = 0x01;
sday = 0x00;
smn = 0x00;
syear = 0x00;
asec = 0x00;
amin = 0x00;
ahr = 0x00;
amn = 0x00;
ayear = 0x00;

while(1) {

if((SET == 1) && (INC == 0) && (DEC == 0) &&(OK == 0)) {
Delay_ms(100);
if((SET == 1) && (INC == 0) && (DEC == 0) && (OK == 0)) {
set_flag = 1;
set_time_flag = 1;
set_cnt++;
if(set_cnt > 11) {
set_cnt = 1;
alarm_flag = 0;
}
else if(set_cnt == 7) alarm_flag = 1;
}
}
else if((INC == 1) && (DEC == 0) && (SET == 0) && (OK == 0) && (set_flag == 1)) {
Delay_ms(100);
if((INC == 1) && (DEC == 0) && (SET == 0) && (OK == 0) && (set_flag == 1)) {
inc_flag = 1;
if(alarm_flag == 0) {
switch(set_cnt) {
case 1:
sweek_day++;
if(sweek_day > 7) sweek_day = 1;
break;
case 2:
sday++;
if(sday == 10) sday = 16;
if(sday == 26) sday = 32;
if(sday == 42) sday = 48;
if(sday > 49) sday  = 1;
break;
case 3:
smn++;
if(smn == 9) smn = 16;
if(smn > 18) smn = 1;
break;
case 4:
syear++;
if(syear == 10) syear = 16;
if(syear == 26) syear = 32;
if(syear == 42) syear = 48;
if(syear == 58) syear = 64;
if(syear == 74) syear = 80;
if(syear == 90) syear = 96;
if(syear == 106) syear == 112;
if(syear == 122) syear = 128;
if(syear == 138) syear = 144;
if(syear > 153) syear = 0;
break;
case 5:
shr++;
if(shr == 10) shr = 16;
if(shr == 26) shr = 32;
if(shr > 35) shr = 0;
break;
case 6:
smin++;
if(smin == 10) smin = 16;
if(smin == 26) smin = 32;
if(smin == 42) smin = 48;
if(smin == 58) smin = 64;
if(smin == 74) smin = 80;
if(smin > 89) smin = 0;
break;
default:
break;

};
}

else if(alarm_flag == 1) {
switch(set_cnt) {
case 7:
break;;
case 8:
amn++;
if(amn > 12) amn = 1;
break;
case 9:
ayear++;
if(ayear > 99) ayear = 0;
break;
case 10:
ahr++;
if(ahr > 23) ahr = 0;
break;
case 11:
amin++;
if(amin > 59) amin = 0;
break;
default:
break;
};
}
}
}
else if((DEC == 1) && (SET == 0) && (INC == 0) && (OK == 0) && (set_flag == 1)) {
Delay_ms(100);
if((DEC == 1) && (SET == 0) && (INC == 0) && (OK == 0) && (set_flag == 1)) {
dec_flag = 1;
if(alarm_flag == 0) {
switch(set_cnt) {
case 1:
sweek_day--;
if(sweek_day < 1) sweek_day = 7;
break;
case 2:
sday--;
if(sday == 15) sday = 9;
if(sday == 33) sday = 25;
if(sday == 47) sday = 41;
if(sday > 49) sday  = 49;
break;
case 3:
smn--;
if(smn == 9) smn = 16;
if(smn > 18) smn = 18;
break;
case 4:
syear--;
if(syear == 15) syear = 9;
if(syear == 33) syear = 25;
if(syear == 47) syear = 41;
if(syear == 63) syear = 57;
if(syear == 79) syear = 73;
if(syear == 95) syear = 89;
if(syear == 111) syear == 105;
if(syear == 127) syear = 121;
if(syear == 143) syear = 137;
if(syear > 153) syear = 153;
break;
case 5:
shr--;
if(shr == 15) shr = 9;
if(shr == 33) shr = 25;
if(shr > 35) shr = 35;
break;
case 6:
smin--;
if(smin == 15) smin = 9;
if(smin == 33) smin = 25;
if(smin == 47) smin = 41;
if(smin == 63) smin = 57;
if(smin == 79) smin = 73;
if(smin > 89) smin = 89;
break;
default:
break;

};
}
else if(alarm_flag == 1) {
switch(set_cnt) {
case 7:
break;
case 8:
amn--;
if(amn > 12) amn = 12;
if(amn < 1) amn = 12;
break;
case 9:
ayear--;
if(ayear > 99) ayear = 99;
if(ayear < 0) ayear = 99;
break;
case 10:
ahr--;
if(ahr > 23) ahr = 23;
if(ahr < 0) ahr = 23;
break;
case 11:
amin--;
if(amin > 59) amin = 59;
if(amin < 0) amin = 59;
break;
default:
break;
};
}
}
}
else if((OK == 1) && (SET == 0) && (INC == 0) && (DEC == 0) && (set_flag == 1)) {
Delay_ms(100);
if((OK == 1) && (SET == 0) && (INC == 0) && (DEC == 0) && (set_flag == 1)) {
set_flag = 0;
set_time_flag = 0;
ok_flag = 1;
alarm_flag = 0;
alarm_set_flag = 1;
set_cnt = 0;
inc_flag = 0;
dec_flag = 0;
}
}

if((inc_flag == 1) || (dec_flag == 1)) {

s_sec =  ssec;
s_min = smin;
s_hr = shr;
s_week_day = sweek_day;
s_day = sday;
s_mn = smn;
s_year = syear;

/*
ssec = s_sec;
smin = s_min;
shr = s_hr;
sweek_day = s_week_day;
sday = s_day;
smn = s_mn;
syear = s_year;
*/

/*
s_sec = Dec2Bcd(ssec);
s_min = Dec2Bcd(smin);
s_hr = Dec2Bcd(shr);
s_week_day = Dec2Bcd(sweek_day);
s_day = Dec2Bcd(sday);
s_mn = Dec2Bcd(smn);
s_year = Dec2Bcd(syear);
*/
}

if(set_time_flag == 1) {
Write_Time();
ok_flag = 0;
}

if(ok_flag == 1) {
}

if(alarm_flag == 0) {
Transform_Time(&sec,&min1,&hr,&week_day,&day,&mn,&year);
Display_Time(sec, min1, hr, week_day, day, mn, year);
if(alarm_set_flag == 1) {
//if((day == aday) && (mn == amn) && (year == ayear) && (hr == ahr) && (min1 == amin)) {
//PORTB.F0 = 1;
//}
if((day == aday) && (mn = amn) && (year == ayear) && (hr == ahr) && (min1 == amin)) {
PORTB.F0 = 1;
}
}
}

if(alarm_flag == 1) {
Display_Alarm_Time(asec, amin, ahr, aweek_day, aday, amn, ayear);
}

}
}

- - - Updated - - -

Finally can I do like this I have 153 decimal in mydec

I do myHex = Dec2Hex(mydec);

and then I2C1_Wr(myHex);

Will this work? Will the value of myHex after Dec2Hex(myDec) will be 0x99?

Will this code
Code:
 ((min/16) << 4) + (min%16)
work?

Code:
myDec = 153;

i.e. myHex = ((myDec/16) << 4) + (myDec%16);

Code:
 I2C1_Wr(myHex);

- - - Updated - - -

@FvM Yes. You are right. I haven't slept properly since 2 days.

- - - Updated - - -

Code:
 If I use DectoBcd(153) I get 99 as result and not 0x99. so how to make that 99 to 0x99?

If I do myHex = DectoHex(153), will the value of variable myHex be 0x99 or 99? I know that DectoBcd(153) is 0x99.

But I want to know what actual value the myHex variable contain? will it be in the form of 0x99 or just 99? I know that in hardware level myHex will be 1001 1001.

Last edited:

Finally can I do like this I have 153 decimal in mydec

I do myHex = Dec2Hex(mydec);

and then I2C1_Wr(myHex);

Will this work? Will the value of myHex after Dec2Hex(myDec) will be 0x99?
For some reason you decided to manipulate BCD numbers rather than decimals when setting the clock. That turns out rather long-winded due to the extra code managing the decimal adjustment with carry to tenth digit, but is basically possible. I suggested multiple times to use decimal numbers for the set operation, e.g. year 0..99, and perform dec2bcd() afterwards.

As you already have BCD numbers. you must not apply dec2bcd() a second time. Just write the number 153 to the RTC register to set year 99.

Finally: DectoBcd(153) isn't a valid operation, argument range of DectoBcd() is 0..99.

If syear is a BCD number (range 0.. 0x99 respectively 0..153), this text would emphasize the intended operation:
Code:
mybcd=syear;
I2C1_Wr(mybcd);

Last edited:
• bigdogguru and tpetar

Points: 2

### bigdogguru

Points: 2
OK. I will have to write 0x99 or d153 for year 99. See my last post. It is updated.

Can I do like this. Instead of
Code:
 I2C1_Wr(153);
I want to do
Code:
myHex = DecToHex(153)
I2C1_Wr(myHex);

myHex will be 0x99 I think. So It will be same as I2C1_Wr(0x99)

- - - Updated - - -

@ FvM Sorry, It was not DecToBcd(153). It was DectoHex(153)

What's Dec2Hex()? You don't need it at all.

Status
Not open for further replies.