Sver
Full Member
Posts: 145
|
Post by Sver on Nov 25, 2023 6:04:13 GMT -5
'Convert Number Base (BIN/DEC/HEX) binair 'Written by Welopez, 07/29/2007
NOMAINWIN WindowWidth=600 WindowHeight=280 UpperLeftX=INT((DisplayWidth-WindowWidth)/2) UpperLeftY=INT((DisplayHeight-WindowHeight)/2)
STATICTEXT #win.st1, "Enter a number for conversion...", 20, 20, 250, 30 TEXTBOX #win.tb1, 20, 50, 550, 30 BUTTON #win.btn1, "BIN2DEC", [bin2dec], UL, 50, 90, 80, 30 BUTTON #win.btn2, "DEC2BIN", [dec2bin], UL, 150, 90, 80, 30 BUTTON #win.btn3, "HEX2DEC", [hex2dec], UL, 250, 90, 80, 30 BUTTON #win.btn4, "DEC2HEX", [dec2hex], UL, 350, 90, 80, 30 BUTTON #win.btn5, "QUIT", [quit], UL 450, 90, 80, 30 STATICTEXT #win.st2, "The converted value is...", 20, 140, 200, 30 TEXTBOX #win.tb2, 20, 180, 550, 30 OPEN "Number Base Conversion" FOR DIALOG AS #win PRINT #win, "trapclose [quit]" PRINT #win, "FONT arial 10 bold" [reDo] '<< return here if we make an "Ooops!" PRINT #win.tb1, "" PRINT #win.tb1, "!setfocus" WAIT
[bin2dec] 'Convert binary num$ to decimal dec$="" PRINT #win.tb1, "!contents? num$" lenBin=LEN(num$) FOR k=1 TO lenBin IF VAL(MID$(num$, k, 1))>1 THEN CALL errorMsg GOTO [reDo] END IF NEXT k
num=bin2dec(num$, lenBin, dec$) PRINT #win.tb2, dec$ WAIT
[dec2bin] 'Convert decimal num$ to binary PRINT #win.tb1, "!contents? num$" dec=VAL(num$) num=dec2bin(num$, dec) PRINT #win.tb2, VAL(num$) WAIT
[hex2dec] 'Convert hexadecimal to decimal PRINT #win.tb1, "!contents? num$" hex$=upper$(num$) lenHex=LEN(hex$) num=hex2Dec(hex$, lenHex, dec$) PRINT #win.tb2, dec$ WAIT
[dec2hex] 'Convert decimal to hexadecimal PRINT #win.tb1, "!contents? num$" num$=UPPER$(num$) lenDec=LEN(num$) FOR k=1 TO lenDec char=ASC(MID$(num$,k,1)) IF char<48 OR char>57 THEN CALL errorMsg GOTO [reDo] END IF NEXT k
hexVal=dec2hex(num$, dec$) PRINT #win.tb2, dec$ WAIT
[quit] CLOSE #win END
FUNCTION bin2dec(num$, lenBin, byref dec$) for k = 1 to lenBin if mid$(num$,k,1)="1" THEN dec=dec+2^(lenBin-k) next k dec$=STR$(dec) END FUNCTION
FUNCTION dec2bin(byref num$, dec) dec=INT(VAL(num$)) num$=""
WHILE dec>0 num$=STR$(dec MOD 2)+num$ dec=INT(dec/2) WEND
END FUNCTION
FUNCTION hex2Dec(byref num$, lenHex, byref dec$) dec$=""
FOR k=1 TO lenHex unit$=MID$(num$,k,1) IF unit$="#" OR unit$="H" THEN [skip] SELECT CASE unit$ CASE "A" : n=10 CASE "B" : n=11 CASE "C" : n=12 CASE "D" : n=13 CASE "E" : n=14 CASE "F" : n=15 CASE ELSE : n=VAL(unit$) END SELECT dec=dec+(n*16^(lenHex-k)) [skip] NEXT k dec$=STR$(dec) END FUNCTION
FUNCTION dec2hex(num$, byref dec$) num=VAL(num$) char$="0 1 2 3 4 5 6 7 8 9 A B C D E F" dec$="" DO temp=INT(num MOD 16) dec$=WORD$(char$,(temp+1))+dec$ num=INT(num/16) LOOP UNTIL num=0 END FUNCTION
SUB errorMsg
NOTICE "Error! Error! Stupid ERROR!"+CHR$(13)+_ "The number you entered is not a proper value."+CHR$(13)+_ "Please try again." END SUB
|
|
|
Post by tsh73 on Nov 25, 2023 6:33:03 GMT -5
Hello Sver For your information, DECHEX$( number ) HEXDEC( hexString$ ) are built-in functions
|
|
Sver
Full Member
Posts: 145
|
Post by Sver on Nov 25, 2023 7:37:58 GMT -5
Tsh73, Other parts can be used. 'binair text
binary$="010000110110100001100101011001010111001001110011" print bin2txt$(binary$) END
function bin2txt$(n$) for s1=1 to len(n$) step 8 for s2=1 to 8 x=x+val(mid$(mid$(n$,s1,8),8-s2+1,1))*(2^(s2-1)) next s2 bin2txt$=bin2txt$+chr$(x):x=0 next s1 end function or rem Convert code to binair text rem Use as you please! rem By Marnix Vs
'Ask text from user:
input "Enter text > "; code.txt$ print bin$(code.txt$); print
[Confirm] input "Do you want to re-translate (Y/N)? > "; answ.trans$ if answ.trans$ = "N" then _ end
if answ.trans$ = "Y" then
print trans$(bin$(code.txt$)) call prog.wait 3 end else goto [Confirm] end if
'Convert each caracter to a binary number. 'The whole ASCII table is one byte consisting 'of eight bits. We'll check each character and convert.
function bin$(txt$) for index = 1 to len(txt$) curr.char$ = mid$(txt$,index,1) asc = asc(curr.char$) if asc = 0 then print "Error! Invalid character."; exit function end if
'The highest number is 255, 'the lowest is 0 byte$ = ""
for i = 1 to 8 if asc mod 2 = 0 then byte$ = byte$ + "0" asc = asc / 2 else
byte$ = byte$ + "1" asc = (asc - 1) / 2 end if next i
'We have the byte now, but its 'reverted, as we begun with the 'highest number!
for n = 1 to 8 bin$ = bin$ + mid$(byte$,9 - n,1) next n
'Add a space for the next byte: bin$ = bin$ + chr$(32) next index end function
'This sub is to let the program wait 'for <n> seconds. This handy when the 'mainwin is used but the program is exe.
sub prog.wait n 'Capture the time: time = time$("seconds") wait.num = time + n
while time$("seconds") <> wait.num 'Nothing goes here! wend
end sub
'This function translates a ASCII byte 'to a valid character (0 - 255).
function trans$(bin$) m = 1 while word$(bin$,m) <> "" char.num = 0 single.byte$ = word$(bin$,m) m = m + 1 'We'll go left to right here. for x = 1 to 8 bit$ = mid$(single.byte$,x,1) select case x case 1 if bit$ = "1" then _ char.num = char.num + 128 case 2 if bit$ = "1" then _ char.num = char.num + 64 case 3 if bit$ = "1" then _ char.num = char.num + 32 case 4 if bit$ = "1" then _ char.num = char.num + 16 case 5 if bit$ = "1" then _ char.num = char.num + 8 case 6 if bit$ = "1" then _ char.num = char.num + 4 case 7 if bit$ = "1" then _ char.num = char.num + 2 case 8 if bit$ = "1" then _ char.num = char.num + 1 end select next x trans$ = trans$ + chr$(char.num) wend
'We're ready, the text has been 'formed, each character apart. end function 'Demo for a Decimal to Binary and Binary to Decimal functions. 'The values to pass into the DecBin$ function are the decimal 'number and the number of bits you want returned. 'The string to pass into the BinDec is a binary$ such as "0101"
for x= 0 to 1023 a$ = DecBin$(x,16) b = BinDec(a$) print b,a$ next end
Function DecBin$(decimalnumber,numberbits)'numberbits would normally be 8,16,or 32 for x = numberbits-1 to 0 step -1 b = 2^x if b > decimalnumber then BinNumber$ = BinNumber$ + "0" else decimalnumber = decimalnumber-b BinNumber$ = BinNumber$ + "1" end if next DecBin$ = BinNumber$ End Function
Function BinDec(binary$) origlength = len(binary$) for x = 1 to origlength a = val(right$(binary$,1)) b = len(binary$) if a > 0 then decimalnumber = decimalnumber+(2*a)^(x-1) end if binary$ = left$(binary$,b-1) next BinDec = decimalnumber end function
|
|