PDA

View Full Version : Need help with binary -> decimal DIGITS


Jeff_B
12-01-2006, 09:02 PM
Not sure if this is the right forum or even if there is a forum here for this type of question but I cant figure this out...
Binary numbers are base two and each 1 indicates that 2^x is to be addded to the number. (x is the index starting from 0 on the far RIGHT)

EXAMPLES 1011 is 2^0 + 2^1 + 2^3 = 11
2^2 is not there because its a 0


Now my issue....
I need to transform this to decimal and get the individual decimal digits. like for

10111 (23) = 2^4 + 2^2 + 2^1 + 2^0 = 16+4+2+1 = 23
So given 10111 I would need to get
tens = 2
ones = 3


Ideally there would be a way to tell from the binary string?

I can NOT use division and I am unable to subtract 10 repeatedly and keep track...


Anyone have ANY idea?

(My goal is to display a 3 digit number given in binary on 3 7 segments displays for a spartan 3 starter board if you are familiar with this)

ScottHoward
12-01-2006, 09:15 PM
1011 does not equal 9

ScottHoward
12-01-2006, 09:20 PM
[ QUOTE ]
1011 does not equal 9

[/ QUOTE ]
other than that, i cant help you /images/graemlins/frown.gif

Jeff_B
12-01-2006, 09:26 PM
[ QUOTE ]
[ QUOTE ]
1011 does not equal 9

[/ QUOTE ]
other than that, i cant help you /images/graemlins/frown.gif

[/ QUOTE ]

Thanks my brain is fried from thinking about this for so long /images/graemlins/frown.gif

gull
12-01-2006, 10:34 PM
Hmmm... I will write my thoughts out. I probably can't help. Why can't you use division?


x = 2 and y = 10.

a_n and b_n are integers in the range [0, a_n * x] and [0, b_n * y]

sum[0,inf](a_n * x^n)= sum[0,inf](b_n * y^n)

Given set a, find set b.

Hmph. I tried some stuff and gave up here.

This code is not what you're asking for and it won't help. It ends with a decimal number, not digits of a decimal number. It's just a fast way to convert from binary to decimal from left to right.

int i=0;
int decimal=0;
while(string[i])
{
decimal = 2 * decimal + ((1==string[i])?1:0);
i++
}

Jeff_B
12-01-2006, 11:03 PM
Yeah not goign to help I actually found an algorithm that isnt too bad that involves doing left shifts over and over and adding 3 whenever something is > 5 (cant explain it without the example too well)

DougShrapnel
12-01-2006, 11:13 PM
Jeff I believe this will work. For division by 10 for all positive numbers < 534,890 using 16 bits. You would have to put it in loop while Q is > 10. Or figure out some other fancy way to get Tens, Hundreds, thousands and so on.

unsigned int A; /*your number*/
unsigned int Q; /* the quotient */
unsigned int R; /* the remainder */

Q = ((A >> 1) + A) >> 1; /* Q = A*0.11 */
Q = ((Q >> 4) + Q) ; /* Q = A*0.110011 */
Q = ((Q >> 8) + Q) >> 3; /* Q = A*0.00011001100110011 */


R = ((Q << 2) + Q) << 1;
R = A - R; /* R = A - 10*Q */
if (R >= 10) {
R = R - 10;
Q = Q + 1;
}


Link (http://www.cs.uiowa.edu/~jones/bcd/divide.html)

Jeff_B
12-01-2006, 11:51 PM
[ QUOTE ]
Jeff I believe this will work. For division by 10 for all positive numbers < 534,890 using 16 bits. You would have to put it in loop while Q is > 10. Or figure out some other fancy way to get Tens, Hundreds, thousands and so on.

unsigned int A; /*your number*/
unsigned int Q; /* the quotient */
unsigned int R; /* the remainder */

Q = ((A >> 1) + A) >> 1; /* Q = A*0.11 */
Q = ((Q >> 4) + Q) ; /* Q = A*0.110011 */
Q = ((Q >> 8) + Q) >> 3; /* Q = A*0.00011001100110011 */


R = ((Q << 2) + Q) << 1;
R = A - R; /* R = A - 10*Q */
if (R >= 10) {
R = R - 10;
Q = Q + 1;
}


Link (http://www.cs.uiowa.edu/~jones/bcd/divide.html)

[/ QUOTE ]


What are the << and >> operands?

not so sure i get this I am reading the link now though

DougShrapnel
12-02-2006, 12:01 AM
"What are the << and >> operands?

not so sure i get this I am reading the link now though"

Binary shifts.

so 52 >> 3 shifts 52 in biary 3 spots in the direction of the arrows.
110100 >> 3 = 000110

OrigamiSensei
12-04-2006, 04:44 PM
Jeff, are you using software or hardware? If you are doing a conversion using an FPGA or CPLD an easy way to do this is with a ROM or lookup table. Assuming the number you want to convert is 8-digit binary a 256x8 ROM will work nicely for each individual digit (technically it's 256x7 and not using the eighth bit of output). You can also do it the hard way by having an individual truth table and logic circuitry for each LED segment but that's not really worth the effort. There are also any number of LED encoder semiconductors out there if you're using discrete parts.

Also, Google is your friend. Just start googling for things like "LED+segment+encoder".