View previous topic :: View next topic |
Author |
Message |
sorasit46
Joined: 07 May 2007 Posts: 69
|
How to convert dec. to hex. in pic16f819 ? |
Posted: Sat Dec 11, 2010 5:05 am |
|
|
Any ideas on how to convert dec. to hex.?
exe. 43 = 0x2B
thank you |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19545
|
|
Posted: Sat Dec 11, 2010 5:14 am |
|
|
Depends what you mean.....
Understand, the PIC, does not 'work' directly in decimal, or hex. It works internally in binary. So when you input a number (typing it in), you have to have code, that accepts the digits, and converts the text sequence, into binary. Once inside the PIC, you can then output this in a huge 'range' of ways. If you 'putc' the value, a single character is sent, who's ASCII value is the number inside the PIC. Or if you use printf, you have then a 'range' of output formats possible. Look at the %x format specifier in printf....
Best Wishes |
|
|
sorasit46
Joined: 07 May 2007 Posts: 69
|
|
Posted: Sat Dec 11, 2010 5:34 am |
|
|
I mean how to write source code for convert dec. value to hex. value in my programe,such as built in function. |
|
|
Johann17943
Joined: 11 Dec 2010 Posts: 11 Location: Germany
|
|
Posted: Sat Dec 11, 2010 5:51 am |
|
|
Hi sorasit46
The function for dec to hex is:
Code: |
int dec_hex(int zahl);
{
int einer,zehner,ergebnis;
ergebnis=0;
zehner=zahl/10;
einer=zahl-(zehner*10);
ergebnis=(zehner<<4)|einer;
return ergebnis;
} |
example:
Code: | hex_value=dec_hex(dec_value); |
|
|
|
sorasit46
Joined: 07 May 2007 Posts: 69
|
|
Posted: Sat Dec 11, 2010 5:56 am |
|
|
Hi! Jonann17943
Thanks for the help!
but I correct some code.
Code: | int dec_hex(int zahl);
{
int einer,zehner,ergebnis;
ergebnis=0;
zehner=zahl/[b]16[/b];
einer=zahl-(zehner*[b]16[/b]);
ergebnis=(zehner<<4)|einer;
return ergebnis;
}
|
My data is int16,not int.
How to modify this source code? |
|
|
|