PxPlus User Forum

Main Board => Discussions => Programming => Topic started by: HendersonS on February 27, 2019, 10:14:49 AM

Title: binary to decimal convertertion
Post by: HendersonS on February 27, 2019, 10:14:49 AM
Hello everyone,

Does anyone know if there is a function in pxplus to convert a binary number (0000000001000000) to a decimal number(64)?, I read about the dec () function but it's not what I want.
Title: Re: binary to decimal convertertion
Post by: James Zukowski on February 27, 2019, 11:22:30 AM
Please clarify:
Is the source value just ones and zeroes?
Is it a string or numeric value?
Title: Re: binary to decimal convertertion
Post by: chrisk on February 27, 2019, 07:10:23 PM
Give this a try:

0010 ! begin
0020 x$="0000000001000000"
0030 print dec($00$+fn_atob$(x$))
0040 end
0100 FN_BTOA:! ^100
0110 ! Function which converts ASCII data to its binary representation
0120 def fn_btoa$(local x$)=hta(tbl(hta(tbl(hta(x$),"0123456789ABCDEF",
0120:$00010203101112132021222330313233$)),"0123",$00011011$))
0200 FN_ATOB:! ^100
0210 ! Function which returns ASCII value corresponding to a string a binary data
0220 def fn_atob$(local x$)=ath(tbl(ath(tbl(ath(x$),$00011011$,"0123")),
0220:$00010203101112132021222330313233$,"0123456789ABCDEF"))
Title: Re: binary to decimal convertertion
Post by: HendersonS on February 28, 2019, 07:51:02 AM
thanks chris I will try this routine.
Title: Re: binary to decimal convertertion
Post by: Mike King on February 28, 2019, 04:23:22 PM
Actually here is another solution:

0010 begin
0020 LET x$="0000000001000000"
0030 LET value=0
0040 WHILE x$<>""
0050 LET value=2*value+NUM(x$(1,1))
0060 LET x$=x$(2)
0070 WEND
0080 PRINT value


Or perhaps as a simple recursive function:

0010 LET x$="0000000001000000"
0020 DEF FN_val(LOCAL x$)=TBL(x$="",NUM(MID(x$,-1))+2*FN_val(MID(x$,-1,-100)),0)
0030 PRINT FN_val(x$)

Title: Re: binary to decimal convertertion
Post by: James Zukowski on February 28, 2019, 05:45:48 PM
Mike:
Does this mean the limit on recursive calls has been eliminated? Or changed?
According to the documentation, one of the reasons for throwing Error #25:

Quote
Cannot exceed limit on number of recursive calls to a user defined function. (Maximum of 10 recursive calls, ten levels deep.)

Or does defining the function with Local variables override the limit?

Thanks in advance for the clarification.
Title: Re: binary to decimal convertertion
Post by: Mike King on March 04, 2019, 11:04:22 AM
No, the limit still exists its just been updated to 100 in PxPlus as of Version 7.
This means the recursive function I posted would only work for binary strings of 100 bytes or less (not likely a problem in most business applications).

We will get the manual updated.