PxPlus User Forum

Twitter Twitter Twitter

Author Topic: binary to decimal convertertion  (Read 2180 times)

HendersonS

  • Gold Member
  • ****
  • Posts: 58
    • View Profile
binary to decimal convertertion
« 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.

James Zukowski

  • Diamond Member
  • *****
  • Posts: 297
    • View Profile
Re: binary to decimal convertertion
« Reply #1 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?
James Zukowski
Sr. Developer - J&E

BRAND>SAFWAY
Brand Industrial Services

chrisk

  • Member
  • **
  • Posts: 14
    • View Profile
Re: binary to decimal convertertion
« Reply #2 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"))
Chris Kukuchka
Sequoia Group, Inc.

HendersonS

  • Gold Member
  • ****
  • Posts: 58
    • View Profile
Re: binary to decimal convertertion
« Reply #3 on: February 28, 2019, 07:51:02 AM »
thanks chris I will try this routine.

Mike King

  • Diamond Member
  • *****
  • Posts: 3811
  • Mike King
    • View Profile
    • BBSysco Consulting
Re: binary to decimal convertertion
« Reply #4 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$)

Mike King
President - BBSysco Consulting
eMail: mike.king@bbsysco.com

James Zukowski

  • Diamond Member
  • *****
  • Posts: 297
    • View Profile
Re: binary to decimal convertertion
« Reply #5 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.
James Zukowski
Sr. Developer - J&E

BRAND>SAFWAY
Brand Industrial Services

Mike King

  • Diamond Member
  • *****
  • Posts: 3811
  • Mike King
    • View Profile
    • BBSysco Consulting
Re: binary to decimal convertertion
« Reply #6 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. 
Mike King
President - BBSysco Consulting
eMail: mike.king@bbsysco.com