Author Topic: Hi-Tech C Binary/HEX to BCD  (Read 5916 times)

0 Members and 1 Guest are viewing this topic.

Offline singlarrysongTopic starter

  • Contributor
  • Posts: 34
Hi-Tech C Binary/HEX to BCD
« on: May 16, 2015, 04:15:02 pm »
Is there any efficient way to convert Binary to Hex in Hi-Tech C compiler?
i did not found any macro in the compuiler

My variable is 0-99(Decimal) and im using char, 8bits wide thus 0000 - 0110 0011(binary).
The only way I figure out so far is to divide the variable by 10 to get the most significant decimal, but problem found that for the less significant decimal will be trouble as I need add in different constant to the value 10- 99 to make the last 4 bit of the variable exactly shown 0-9 for any value greater than 9.
 

Offline IanB

  • Super Contributor
  • ***
  • Posts: 13125
  • Country: us
Re: Hi-Tech C Binary/HEX to BCD
« Reply #1 on: May 16, 2015, 04:27:49 pm »
What do you mean by "convert"? Do you mean print?

Also, from what you wrote, two digits 0-99, and dividing by 10, what do you mean by "Hex"? Do you mean BCD?

Your question is very confusing. I don't understand what you trying to do.
 

Offline singlarrysongTopic starter

  • Contributor
  • Posts: 34
Re: Hi-Tech C Binary/HEX to BCD
« Reply #2 on: May 16, 2015, 04:58:34 pm »
Sorry about the confusion.
I need to print 0-99 on two 7-seg drive by 4511 decoder which decode BCD input to match 7-seg input.
And I'll have a char variable that increment or decrement in software.
Since in system language it usually binary or hex, thus I would like to know any kinds of method to convert them into BCD. So I can set(print) the output to match the 4511 input.
 

Offline IanB

  • Super Contributor
  • ***
  • Posts: 13125
  • Country: us
Re: Hi-Tech C Binary/HEX to BCD
« Reply #3 on: May 16, 2015, 05:07:23 pm »
You can divide by 10 to get the most significant digit, then you can subtract that from the original number to get the least significant digit. Like this:

    d1 = n/10;        /* n holds original value, d1 is most significant digit */
    d2 = n - 10*d1; /* d2 is least significant digit */

Example:

   n is 57
   57/10 is 5 -> first digit
   57 - 10*5 is 7 -> second digit
 

Offline Someone

  • Super Contributor
  • ***
  • Posts: 6132
  • Country: au
    • send complaints here
Re: Hi-Tech C Binary/HEX to BCD
« Reply #4 on: May 16, 2015, 11:34:18 pm »
Binary to BCD is a well researched problem, there are many implementations floating around if you use your favourite search engine. Bonus points for finding an algorithm well suited to your target platform and application (which integer divide & multiply most certainly wont be).
 

Online oPossum

  • Super Contributor
  • ***
  • Posts: 1559
  • Country: us
  • Very dangerous - may attack at any time
Re: Hi-Tech C Binary/HEX to BCD
« Reply #5 on: May 17, 2015, 10:17:21 am »
The easy way to do it in C is with the C standard library function div();

Code: [Select]
#include <stdlib.h>

int x = 42;  // Some number from 0 to 99

div_t d = div(x, 10);

d.quot is first digit
d.rem  is second digit

this method can easily be extended to more digits

Code: [Select]

int x = 1234; // some number from 0 to 9999

div_t d = div(x, 1000);

d.quot is first digit

d = div(d.rem, 100);

d.quot is second digit

d = div(d.rem, 10);

d.quot is third digit
d.rem  is fourth digit
 

Offline singlarrysongTopic starter

  • Contributor
  • Posts: 34
Re: Hi-Tech C Binary/HEX to BCD
« Reply #6 on: May 17, 2015, 10:17:48 am »
Thanks for everyone reply, will look for it in two days time.
Recently busy. Sorry about that :)
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

- - -
-->