Go Down

### Topic: Binary Equation (Read 12144 times)previous topic - next topic

#### fuzzball27

##### Jul 19, 2011, 02:34 am
Is there an algebraic equation that Arduino can run to convert from base 10 to binary form? (and binary to base 10; algebraic)
fuzzball27 >>-->

#### retrolefty

#1
##### Jul 19, 2011, 03:00 am
Well the serial print command can told to output a variable in either decimal or binary value so the code must be around inside the core somewhere.

#### Utopia

#2
##### Jul 19, 2011, 07:41 am
Internally, values are usually binary (although you can assume they are anything you want,like ascii, bcd, etc.) So input routines take a decimal number and convert it to binary. Output routines take the binary and output as decimal.

If you want an algorithm for them, it's somewhat like this:

internal to base 10:
take the binary number
divide by 10, saving the remainder (which is a digit of the result) and the result
keep doing that to the result, successively peeling off digits until the result is zero
take this digits and output them in the reverse order you generated them

base 10 to internal:
take the input character, and convert to a digit (0-9)
multiply the internal number by 10,and add this digit
repeat to the internal number for each digit until all the input is 'used up'

#### robtillaart

#3
##### Jul 19, 2011, 11:08 am
Quote
Is there an algebraic equation that Arduino can run to convert from base 10 to binary form? (and binary to base 10; algebraic)

What is the datatype of the input and output??
If it was a function how would the prototype of it look like? e.g  String(int x, int toBase); ...
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

#### fuzzball27

#4
##### Jul 19, 2011, 02:00 pm
Let me rephrase: "Is there an algebraic equation to convert between base10 and binary?"
fuzzball27 >>-->

#### JChristensen

#5
##### Jul 19, 2011, 05:46 pm
This is one of those questions that is difficult to answer out of context, which is why folks are answering with more questions.  The answer depends on where the base 10 and binary numbers are, and how they are being used, so a little more background would help.  I'm not sure about the "algebraic equation" part.  Algebra deals with variables and numbers, and the base is largely irrelevant.  The algebraic equation a = b + c is valid regardless of the base of the variables.

So I'm not sure there is such a thing as an algebraic equation to do base conversion. But as others pointed out, there certainly are algorithms to do base conversion, and some are even built into the Arduino environment.

On the other hand, the internal representation of a number in hardware is always binary.  I like to say (usually when the boss is really steamed about something) "It's all just ones and zeroes."  Or as Tom Lehrer said, "Base 8 is just like base 10 really, if you're missing two fingers."

Ultimately, for humans to be able to read and understand a number from a computer, it has to be converted to characters and displayed on some device.  As @retrolefty indicated, the Arduino Serial.print() function can take a number stored in the MCU and convert it to a character representation in any of several bases: binary (base 2), octal (base eight), decimal (base 10), or hexadecimal (base 16).

If the problem here is to output a number in a given base, then Serial.print() is one way.  On the other hand, if the problem is for a person to input a number in a given base, and convert it to the proper internal representation, then that is a different algorithm.

Tom Lehrer:
New Math

#### borref

#6
##### Jul 19, 2011, 07:20 pm

You're being too nice - the question as asked makes no sense whatsoever.

Is there an algebraic equation to convert between base10 and binary?

The issue here is that "base10" and "binary" are terms related to visual representation whereas algebra involves numeric quantities. Whether the quantities are visualized as binary, octal, hex or decimal has no bearing on algebra. He may as well ask for an equation to convert between blue and red numbers or bold and normal numbers.

#### JChristensen

#7
##### Jul 19, 2011, 09:15 pmLast Edit: Jul 19, 2011, 09:17 pm by Jack Christensen Reason: 1

You're being too nice - the question as asked makes no sense whatsoever.

Could be.  I may just be having one of those days XD  Might have asked a question like that myself once, don't exactly remember though lol...

Quote
The issue here is that "base10" and "binary" are terms related to visual representation whereas algebra involves numeric quantities. Whether the quantities are visualized as binary, octal, hex or decimal has no bearing on algebra. He may as well ask for an equation to convert between blue and red numbers or bold and normal numbers.

Good analogy!

#### Utopia

#8
##### Jul 19, 2011, 09:22 pm

Is there an algebraic equation that Arduino can run to convert from base 10 to binary form? (and binary to base 10; algebraic)

If you mean an algorithm (steps to follow) I mentioned one earlier, and I can provide code if you wish. If however, you really mean algebraic, then no - that is, if you're hoping for a formula (not a function) that gives you

f(15)=1111

I know of nothing. A function, on the other hand...

#### fuzzball27

#9
##### Jul 19, 2011, 11:45 pm
haha oh how I love programming nerd conversations!
Thanks for the help.
fuzzball27 >>-->

#### Grumpy_Mike

#10
##### Jul 20, 2011, 12:05 am
Quote
how I love programming nerd conversations!

Well if you don't ask a sensible question all we can do is to guess what you actually mean. The answer to your original question is of course no there isn't.
But trying to be helpful we try and second guess why you would want to do this anyway, this leads on to the discussion you have seen.

#### robtillaart

#11
##### Jul 20, 2011, 12:20 am

Quote
how I love programming nerd conversations!

I take that as a compliment, we didn't help solve your problem but still made you happy
(Or are you a textwriter looking for inspiration for some sitcom?

Quote
Let me rephrase: "Is there an algebraic equation to convert between base10 and binary?"

It is still not clear what you want, Please give an example how such an algebraic equation looks like, and where the base 10 and binary fits in....

There are 10 types of people in this world those that know binary and those that don't.
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

#### fuzzball27

#12
##### Aug 03, 2011, 07:04 pm
Sorry for the delayed reply. Because of this conversation I realized the difference between value and representation of value. I guess the best way to answer my original question would be: a = a  xD
fuzzball27 >>-->

#### Grumpy_Mike

#13
##### Aug 03, 2011, 07:43 pm
Quote
I realized the difference between value and representation of value.

Or to put it the other way round, a computer manipulates bit patterns, it is the interpretation of those bit patterns that give them meaning.

For example a bit pattern can have a numeric value if you interpenetrate it one way and that bit number can be expressed in any number of different number basis. However the same bit pattern can be interpreted in a different way to mean a number that ranges over positive and negative values. The same bit pattern might be interpreted as a floating point numeric value or a fixed point numeric value or a BCD (binary encoded decimal) value or even an alphabetic character according to any one of a number of conventions like ASCII, Murry Code, Mores Code ... the list goes on.

Just remember it is only bit patterns not numbers that a computer manipulates.

#### ArduinoM

#14
##### Sep 04, 2011, 07:44 pm
try this:
http://www.wikihow.com/Convert-from-Decimal-to-Binary
(this is a 10bit conversion scheme)

in an arduino sketch you would use / (division) and % (modulo)  on integers in a while loop (or for, or any flavour of loop you desire)

you would not find an algebraic function that is not recursive, to do your work.

D.

Go Up