hi,
how to change the value of hex to decimal
For example from hex "0x15" to decimal 15 or in hex like 0x0f.
do we have any option in keil or any other way.
Thankyou
arun.
"do we have any option in keil or any other way."
Option for what?
1) 0x15 is not decimal 15 but decimal 21.
2) Hexadecimal or decimal - it's still the same number. So for your questions to be meaningful, you have to specify how, when, where.
Do you have numbers you need to read in? A function like strtol() can handle decimal, octal or hexadecimal automatically if the hexadecimal numbers always starts with 0x or 0X and the octal numbers always starts with 0 and decimal numbers do not start with 0 (note decimal 0 and octal 0 is the same number so no ambiguity).
DO you need to print numbers? printf() can print the number in hex or decimal.
For source code constants, you don't need any conversions, since the compiler will treat the constants as numbers and don't care about if the constant was represented as decimal or hexadecimal in the source code.
Or you want to write a pocket calculator, where you yourself convert between numeric bases digit-by-digit? That is easy too, and can be done in more than one way.
"how to change the value (sic) of hex to decimal"
The value of a number (or a variable) is the same irrespective of what format you express it in!
eg, 0x15 (hex) has the same value as 21 (decimal) which has the same value as 25 (octal) has the same value as 10101 (binary) has the same value as XXI (roman numerals)
Presumably, what you mean is to convert the value of a variable into text for printing or display or similar - yes?
If so, think about what standard 'C' library functions perform conversions of numbers to formatted text...
Or, if you want to code it yourself, think about how you would do this manually - using pencil and paper...
www.8052.com/.../67454
how to change the value of hex to decimal For example from hex "0x15" to decimal 15 or in hex like 0x0f.
Like this?
unsigned char bcd2int(unsigned char bcd) { return 10 * (bcd >> 4) + (bcd & 0xF); }
I think some processors even have instructions that do just that.
The problem with the above code is that we don't know if the OP know what BCD is. Was 0x15 -> 15 attempted to be a BCD to decimal conversion or an accidental evaluation error?
We constanly suffer from people that don't want to describe what problem they have. They first want to make a couple of own assumptions and then asks a half-mashed question that can't be answered because of lack of information, and often shouldn't even be answered since it is based on the OP being half-way down the wrong path.
If BCD was intended, then the next thing is if there is a need for error recovery - is there a chance that the above function may be called with an invalid BCD value that must be correctly detected? 0xff -> 165 wouldn't be so fun, but isn't a problem if there is screening of the values earlier in the process.
We constanly suffer from people that don't want to describe what problem they have.
I think suffering is the wrong word. To me it's fun :-)
For example, see my posts of 15-Jun-2011 12:52 GMT and 13:40 GMT in this thread: http://www.keil.com/forum/19091/
There seems to be a fundamental problem in distinguishing between a requirement (or "goal"), and an attempted solution to that requirement.
www.catb.org/.../smart-questions.html
First sorry if i irritated you all.
I know how to convert hex "0x15" to decimal 21.
I am using a RTC DS1307 with 89s52 and displaying time in LCD.
have to write and read in hex only with the RTC.
I can write and read the values in RTC and also displayed it in LCD.
The problem for me is when i try to edit the time.
I receiving it in hex and if i directly send it to LCD and edit, the value after 09 became some other characters. if i convert it to decimal and send to LCD it works.
But in the first place itself, if i read 0x15 it displays 21 because i converting it to decimal.
That's why i need to change the hex '0x15' to decimal 15 not 21.
I think you may understand my problem. if not again sorry.
i thought keil will have some option for this, like the typecast.
In other words, you are using BCD or Binary-Coded Decimal - or, strictly, packed BCD.
In packed BCD, each nibble of the binary data holds one digit of the decimal value.
So, all you need to do is to extract the individual nibble values, and display them as the digit values.
You can extract the individual nibble values by using the standard 'C' shift & mask bitwise operators.
This really is a very common task indeed - you should easily be able to find plenty of examples...
Thank you for the all
Thank you Mike Kleshov, i solved my problem from your reply.
Regards arun
Mike's post shows how to convert BCD to a numeric value.
If you just need to display the value, there isn't really any point in converting to a number - you could just extract the digits and then display them directly.
"have to write and read in hex only with the RTC."
Not at all. Hexadecimal is a presentation of a number. Something you may have in your source code. Or dispayed on the LCD. Or printed on a paper. But you do not program the RTC with any hexadecimal data, for the simple reason that the RTC want to see bytes without caring about the existence of any radix or numeric base you may have in your source code.
In this specific case, it's BCD-encoded data. When printing to display, you can use %02x to print it, giving a correct 2-digit presentation of the BCD-encoded value. Or you can use the previous source code to convert into a number and print it using %02u.
If you implement code to edit/input the time, then you can select if your code should use a number 0..59 for minutes and convert to BCD just before sending to the RTC. Or if the code should directly have a BCD value in a byte and (if you have up and down buttons) code that knows that 0x09 + 1 should be 0x10, i.e. that when low nibble increments from 9 it has to be reset to 0 while the high nibble must be incremented. And similar when low nibble is decremented while already being 0.