![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
pavelustinov
Joined: 09 Mar 2013 Posts: 20
|
Convert from hex to int |
Posted: Fri Apr 12, 2013 6:17 am |
|
|
Hello everybody!
I store data "23" in EEPROM like this
Code: | write_eeprom(1, 0x32);// save 2
write_eeprom(2, 0x33);// save 3
first = read_eeprom(1);
second = read_eeprom(2);
|
How can I convert to int and make
first*10 + second = 23
2*10 + 3=23 |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19369
|
|
Posted: Fri Apr 12, 2013 8:42 am |
|
|
Start with the simple thing that the computer does not store hex. It works just in binary, which can then be represented by hex, octal, decimal etc..
So you have one cell containing the number 50, and one containing the number 51. These are the ASCII codes commonly used for the digits '2' and '3'.
Simplest way is just to subtract the offset between these values and the numbers they represent (48 - this is ASCII '0'), multiply and add.
So:
Code: |
int8 val;
#define VALUE_OF(x) x-'0'
first = read_eeprom(1);
second = read_eeprom(2);
val=(VALUE_OF(first)*10)+VALUE_OF(second);
|
There is a generic conversion routine that can do this, but to use it, you would have to transfer the bytes into a character array, add a terminating '\0', and then call the routine. More work than just doing it manually.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
pavelustinov
Joined: 09 Mar 2013 Posts: 20
|
|
Posted: Fri Apr 12, 2013 2:11 pm |
|
|
Ttelmah wrote: | Start with the simple thing that the computer does not store hex. It works just in binary, which can then be represented by hex, octal, decimal etc..
So you have one cell containing the number 50, and one containing the number 51. These are the ASCII codes commonly used for the digits '2' and '3'.
Simplest way is just to subtract the offset between these values and the numbers they represent (48 - this is ASCII '0'), multiply and add.
So:
Code: |
int8 val;
#define VALUE_OF(x) x-'0'
first = read_eeprom(1);
second = read_eeprom(2);
val=(VALUE_OF(first)*10)+VALUE_OF(second);
|
There is a generic conversion routine that can do this, but to use it, you would have to transfer the bytes into a character array, add a terminating '\0', and then call the routine. More work than just doing it manually.
Best Wishes |
I try this
Code: | write_eeprom(1, 0x32);
write_eeprom(2, 0x30);
first = read_eeprom(1);
second = read_eeprom(2);
val=(VALUE_OF(first)*10)+VALUE_OF(second);
lcd_putc(val); |
But LCD doesn't display anything. Am I use this code correctly? |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19369
|
|
Posted: Fri Apr 12, 2013 3:07 pm |
|
|
No.
You now have a variable called 'val' containing the number 20. You send this to the display. The number 20, is the character ' '. A space. So you will see nothing.....
You need to get a basic C textbook and understand the difference between the textual representation of a number, and the number itself. Given the original two characters _were_ the textual representation, why not just print these?. You have taken the text, converted it into the numeric representation, to print this, you need to format it as text (read up on what printf does). |
|
![](templates/subSilver/images/spacer.gif) |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|