Page 1 of 1

Binary decimal conversion program

PostPosted: Tue Apr 28, 2015 2:53 pm
by ohubau
Hello,
I desperately searches for a binary decimal conversion program and vice versa for hp41CX, someone does it in his library! Or can you refer me.
With many thanks.
Olivier Hubau
PS: I can explain why I always use a HP41CX when I lost Math Prodigies, but that's a long story ....
Translated with Google translate, thank you to excuse the inaccuracies

Re: Binary decimal conversion program

PostPosted: Tue Apr 28, 2015 4:14 pm
by Garth
I can't take the time right now to write a program, but the Advantage module has the conversion built in, and it runs with no visible delay.

However, here's an explanation of how to do such a conversion, simplified not quite to the point of lying. :lol: It should give the basic understanding so you can write suitable routines. For inputting numbers from a text string (e.g. typed in from a keyboard), initialize a number as 0 to build on, then take the first digit in the string and add its value to the decimal number you're building. Continue until you're out of digits, each time multiplying the build by 10 and then adding the new digit's value to the build. If you encounter a decimal point, keep track of how many digits were after it. In the Forth programming language, the decimal point automatically makes the result double-precision; but you can convert back to single if you want to. If there was a minus sign, record that too. Forth uses the same routines to convert to and from any base, with the desired base being in a variable called BASE, and it's just as easy to convert to and from base 3 or 7 or 11 for example as it is to convert to and from base 2 or 8 or 10. (Internal representation is hexadecimal.) It's not the fastest method, but it's short and works for any base.

For converting numbers to other bases for output (which will normally be a string), initialize a blank string. You will build it from right to left. Divide your number by what's in variable BASE, and use the remainder to add a text digit to the build, even if it's a 0. Keep doing that until there's 0 in the number. You can add a decimal point or other characters between digits, e.g. 12.345 or 12:36:40 (actually you might want to change BASE from 10 to 6 and back for the time readout, if you started with a number of seconds!)

The way Forth does this output number formatting is somewhat explained starting at about the middle of the page of chapter 5 of Leo Brodie's "Starting Forth" (with ANS updates here, and they're mostly calling single-precision 32-bit).

Re: Binary decimal conversion program

PostPosted: Thu Apr 30, 2015 12:35 pm
by ohubau
Thank you very much for that, but as I tried to explain in PS, I had an accident with head trauma at the point stimulated by the math, so I do not understand almost anything, but I continues to use HP41CX laziness for not having to re-learn to use another calculator, so I would like to know if there is a ready routine because I have not enough memory (mine not that of HP ) to retain the one you made me very kindly presentation. I feel ridiculous, but that's how!
I'm sorry, and pray sincerely apologize.
Thank you a lot.
PS : Translated with Google translate, thank you to excuse the inaccuracies

Re: Binary decimal conversion program

PostPosted: Wed May 06, 2015 12:37 am
by Garth
Ok, I was able to take a few minutes and write one. Start with the binary number in ALPHA (all valid digits for the desired base, so in this case only 0's and 1's, no spaces), then execute the program. At the end, the result will be in X.

Code: Select all
LBL "B2D"   \ I called it "B2D" for "Binary To Decimal"
LBL 00
  ALENG     \ How many characters?
  .00001    \ This is for the DSE function for loop control.
  +
  STO 00
  0         \ Initialize the number we'll build upon as 0.
LBL 01
  2         \ This is the base you want to convert from.
  *         \ Multiply the previous result by the base.
  ATOX      \ Get the left-most character in ALPHA.  Must be valid.
  48
  -         \ Subtract the ASCII value of "0",
  +         \ and add the result to the number we're building.
  DSE 00    \ Decrement the loop counter and compare to the number of characters.
  GTO 01    \ If we're not done, go back for another lap.
  RTN


I've tried it for numbers up to 111111111111111111111111 (24 1's, which is the maximum number of characters accepted in ALPHA), which gives an answer of 16,777,215, or $FFFFFF. After seeing how it works, you can modify it for other bases, skip leading spaces, stop when it encounters an invalid character, use a text file (which allows longer strings) or strings in two sections to get more binary input digits, etc., to make it more versatile.