[SOLVED] converting hex to dec in mikroc

Status
Not open for further replies.

polona1010

Member level 1
Joined
Apr 17, 2013
Messages
40
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,524
How to convert hex value to decimal in mikroc ?

why they forget to implement hex2dec in compiler is there is some special reasson?
 

You will have to be more specific about what you want to do. There is no difference between hex numbers and decimal numbers (or any other radix), they are all stored as binary, it's how you use them in your program that decides how they are interpreted.

Brian.
 

hex and dec are not the same, hex have ABCDEF letters and dec dont have that.

Value hex 64 convert to 100 dec. How to do that?
 

0x2ABC
d10940


= 2*16^3 + A * 16^2 + B * 16^1 + C * 16^0

= 8192 + 2560 + 176 + 12

take each byte from right and check if > 9. If > 9 then substitute

10 for A
11 for B
12 for C
13 for D
14 for E
15 for F

then use (from right to left)

counter = 0

for i = 0 to no of bytes in hex value
if(counter == 0) { sum = sum + num * pow(16, 0);
if(counter == 1) sum = sum + num * pow(16, 1);
if(counter == 2) sum = sum + num * pow(16, 2);
if(counter == 3) sum = sum + num * pow(16, 3);
if(counter == 4) sum = sum + num * pow(16, 4);
if(counter == 5) sum = sum + num * pow(16, 5);
if(counter == 6) sum = sum + num * pow(16, 6);

...

if(counter == N) sum = sum + num * 16^N;




betwixt is right. if a value of variable is 0x10 it is also dec 16, bin 0001 0000. What he wants to do is he has a hex value like AFC4DBA6D and he want to display the decimal value of that on LCD or maybe the hex value is in a string form that has to be displayed as decimal number.
 

I think after 40+ years working in electronics I know the difference between decimal and hexadecimal. :-o

Hex 0x64 and decimal 100 are exactly the same number so what is there to convert?

What I was trying to explain is that the computer has no concept of what radix you are using, it only understands binary. What you see on your PC/LCD or whatever is a representaton of the binary in a form pleasing to you.
As Jayanth points out, hex 0x10, buinary 0001000, decimal 16 and even octal o20 are exactly the same number. What do you mean when you want to convert it? If you want to enter numbers in hexadecimal and display them in decimal look at the 'sscanf', 'fscanf', 'sprintf', 'printf' and 'fprintf' fuunctions which allow you to convert numbers in text form into different bases.

Brian.
 
How to read internal eeprom address where is writen value 64 in hex, and that value to convert into dec value 100 and show that dec value on LCD ?

I know to do with simple math :

64 4 x 1 = 4
64 6 x 16 = 96
============
SUM = 100

But I need help to do this in mikro c
 

0x64 = dec 100 to display 0x64 or dec 100 as 100 on LCD you need to convert each bit of value into character like

100 to LCD = 0 + 0x30 , 0 + 0x30, 1 + 0x30 or 0 + 48, 0 + 48, 1 + 48 becomes '1' '0' '0'

In mikroC there is InToStr(), LongToStr(), FloatToStr(). Use them


Code C - [expand]
1
2
3
4
5
6
7
8
float fltval = 236.27843539;
unsigned char strflt[17];
 
void main(){
         FloatToStr(fltval, strflt);
         LCD_Out(1,1,strflt);
 
}

 




Hi Polona,

Here is one small example in MikroC :

Code:
char decvalue, broj;
char bb[4];

decvalue = 100;             //  100 dec is in 64 hex and 64 is writen
EEPROM_Write(0xFF, decvalue);  (Writes 64 to memory address 0xFF (100 dec = 64 hex)
Delay_ms(20);     // Minimum 20ms must be between write and read process
broj = EEProm_Read(0xFF);  // Read EEPROM address of 0xFF current value is 64 (100 dec = 64 hex)
ByteToStr(broj, bb);     // Convert  broj to bb as string
Lcd_Out(2,1,bb);    // Show on LCD identical value in dec   (100 dec written and on LCD is 100)

Dont forget to include libraries.

Read EEPROM with uC programmer, and see last place in EEPROM 0xFF there is your value written. If you define decvalue = 125 then in EEPROM 0xFF address you will see 7D and on LCD is 125 decimal.



I hope this help.


:wink:


Additional material:
----------------------------------------------------------------
Useful link for converting HEX to DEC for free time:
https://www.permadi.com/tutorial/numHexToDec/
 
Thank you everyone, tpeter code works thanks again.
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…