Accepting decimal value as hex value

Reply

Join Date: Apr 2007
Posts: 3
Reputation: lats is an unknown quantity at this point 
Solved Threads: 0
lats lats is offline Offline
Newbie Poster

Accepting decimal value as hex value

 
0
  #1
Apr 24th, 2007
hi friends,
This is my very 1st message on this site. Can someone please help me out in designing a small function in c programming to accept decimal value as hex value, i mean lets say if user inputs a decimal value say 49, the processor should read(and futher processes) it as 0x49(hex value).

Thanks .

With regards
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 319
Reputation: Luckychap is on a distinguished road 
Solved Threads: 42
Luckychap's Avatar
Luckychap Luckychap is offline Offline
Posting Whiz

Re: Accepting decimal value as hex value

 
0
  #2
Apr 24th, 2007
First of all you do what you want, the processor will always treate any data as Binary i.e. 0101010010001 etc. It does not read hexa numbers. Any way if want your wish to be true then start developing your own Processor.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 3
Reputation: lats is an unknown quantity at this point 
Solved Threads: 0
lats lats is offline Offline
Newbie Poster

Re: Accepting decimal value as hex value

 
0
  #3
Apr 24th, 2007
thanks for replying this bull shit Luckychap. First read properly what i have written then reply with your bull shit. I said i want to convert 49 decimal to 0x49 hex (or 1001001 in binary). normally proccessor would accept the 49 decimal value as 0x31 hex value. (or 110001 in binary).........Got it !!!!!!!!!!!!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Accepting decimal value as hex value

 
0
  #4
Apr 24th, 2007
do you want to convert to a string? sprintf() can do that.
  1. char hexval[8] = {0};
  2. int n = 49;
  3.  
  4. sprintf(hexval,"0x%X", n);
Last edited by Ancient Dragon; Apr 24th, 2007 at 12:14 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 3
Reputation: lats is an unknown quantity at this point 
Solved Threads: 0
lats lats is offline Offline
Newbie Poster

Re: Accepting decimal value as hex value

 
0
  #5
Apr 24th, 2007
Thanks Ancient Dragon for replying. Actually i'm using a 8-bit microcontroller AT89c51 to design a project . Program Memory of this controller is very low only 4kB. I tried using sprintf but it consumed more than 2kb itself. can there be a easier method for the same. Thanks once again,
Regards.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Accepting decimal value as hex value

 
0
  #6
Apr 24th, 2007
I don't think C language is the best for your situation. With such small amount of memory you may need to use assembly. Depending on your compiler C's startup code can be fairly large.

Here is a thread that may help you.
Last edited by Ancient Dragon; Apr 24th, 2007 at 1:12 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Accepting decimal value as hex value

 
0
  #7
Apr 24th, 2007
Here is a solution -- taken from that link I provided
  1. char DecimalToHexa(int rem);
  2. void hexaDecimal(int h, char* buffer, int bufferSize);
  3.  
  4. int main()
  5. {
  6. char buffer[8] = {0};
  7. int deci=115;
  8. hexaDecimal(deci, buffer, sizeof(buffer));
  9. printf("buffer = '%s'\n", buffer);
  10. return 0;
  11. }
  12.  
  13. void hexaDecimal(int h, char * buffer, int bufferSize)
  14. {
  15. int rem;
  16. char output[8]= {0};
  17. char digit = 0;
  18. int i = 0;
  19. int j = 0;
  20. int k = 0;
  21.  
  22. do
  23. {
  24. rem = h%16;
  25. digit = DecimalToHexa(rem);
  26. h=h/16;
  27. output[i] = digit;
  28. ++i;
  29. } while(h/16!=0);
  30. if(h > 0)
  31. {
  32. rem = h;
  33. digit=DecimalToHexa(rem);
  34. output[i] = digit;
  35. ++i;
  36. }
  37. if(i >= bufferSize)
  38. {
  39. buffer[0] = 0;
  40. }
  41. else
  42. {
  43. for(j= i-1, k = 0; j >= 0; j--,k++)
  44. {
  45. buffer[k] = output[j];
  46. }
  47. buffer[i] = 0;
  48. }
  49. }
  50.  
  51.  
  52. char DecimalToHexa(int rem)
  53. {
  54. char c = 0;
  55. if( rem >= 10)
  56. {
  57. c = (rem - 10) + 'A';
  58. }
  59. else
  60. c = rem + '0';
  61. return c;
  62. }
Last edited by Ancient Dragon; Apr 24th, 2007 at 1:20 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC