Vbsep2007 Decimal 2 Binary

  • November 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Vbsep2007 Decimal 2 Binary as PDF for free.

More details

  • Words: 420
  • Pages: 2
private sub cmdclear_click() txtdec.text = "" txtbin.text = "" end sub private sub cmdconvert_click() 'find out if you are converting decimal to binary andif so, convert them if txtbin.text = "" and txtdec.text <> "" then 'retrieve decimal value dec = val(txtdec.text) 'create a duplicate to process, and make 'it compatible - to reduce errors copied = dec + 1 'self adjusting max-bit maxbit = 32 10 if dec < 2 ^ (maxbit - 1) then let maxbit = maxbit - 1: goto 10 'create an array to hold each bit redim bin$(maxbit) 'decimal => binary conversion engine for t = maxbit to 1 step -1 if copied > 2 ^ (t - 1) then copied = copied - (2 ^ (t - 1)) bin$(t) = "1" else bin$(t) = "0" end if next t 'construct binary string from each bit binary$ = "" for t = maxbit to 1 step -1 binary$ = binary$ + bin$(t) next t 'display results txtbin.text = binary$ end if

'convert binary to decimal if txtdec.text = "" and txtbin.text <> "" then 'get binary digit binary$ = txtbin.text 'workout how many bits it contains maxbit = len(binary$) 'create an array to hold each bit redim bin$(maxbit) 'needed to reverse the array redim other$(maxbit) for t = 1 to maxbit 'put each bit in its own string other$(t) = mid$(binary$, t, 1) next t

for t = 1 to maxbit 'reverse the array bin$(t) = other$((maxbit + 1) - t) next t 'empty the variable dec dec = 0 for t = maxbit to 1 step -1 'add up the binary and put it in a decimal if bin$(t) = 1 then let dec = dec + (2 ^ (t - 1)) next t 'output the decimal in it's textbox txtdec = dec end if end sub private sub cmdquit_click() end end sub private sub txtbin_change() 'check inputted value, discard all non binary digits value$ = right$(txtbin.text, 1) if value$ <> "0" and value$ <> "1" and value$ <> "" then txtbin.text = left$(txtbin.text, (len(txtbin.text) - 1)) txtbin.selstart = len(txtbin.text) end if end sub private sub txtdec_change() 'remove all non decimal digits value$ = right$(txtdec.text, 1) if value$ <> "0" and value$ <> "1" and value$ <> "2" and value$ <> "3" and value$ <> "4" and value$ <> "5" and value$ <> "6" and value$ <> "7" and value$ <> "8" and value$ <> "9" and value$ <> "" then txtdec.text = left$(txtdec.text, (len(txtdec.text) - 1)) txtdec.selstart = len(txtdec.text) end if end sub

Related Documents