•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Legacy and Other Languages section within the Software Development category of DaniWeb, a massive community of 374,571 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,550 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Legacy and Other Languages advertiser:
One more look at BCX basic code to show the qbasic programmers how they can modernize. This shows a function that converts an integer value to a binary string. Notice that with BCX basic you need to declare your variables. Makes the code a little easier to read too. Variable, Function and Sub names are case sensitive, keywords are not.
' converting a decimal integer to a binary string ' BCX basic ' ' ' postfix $ indicates a string variable ' could be written as: dim A as String ' DIM A$ A$ = dec2bin$(123) PRINT "decimal 123 = binary ", A$ A$ = dec2bin$(255) PRINT "decimal 255 = binary ", A$ pause ' wait ' ' postfix $ in function name indicates that the function returns a string ' could be written as: function dec2bin Optional (a, digits=0) as String ' keyword Optional indicates parameter digits is optional and defaults to 0 ' variables declared within the function are local ' FUNCTION dec2bin$ Optional (a, digits=0) DIM k, tmp, t ' default to integer variables DIM T$ ' string variable ' don't know the number of digits in a, so figure it out IF digits = 0 THEN tmp = a WHILE tmp > 0 tmp = tmp >> 1 ' shift right by 1 place digits++ ' increment by 1, same as: digits = digits + 1 WEND END IF t = 1 ' do the conversion to zeros and ones FOR k = 1 TO digits tmp = a AND t IF tmp = 0 THEN T$ = "0" & T$ ELSE T$ = "1" & T$ END IF t = t << 1 ' shift left by 1 place NEXT FUNCTION = T$ ' result is in T$, return this END FUNCTION
Post Comment
•
•
•
•
DaniWeb Marketplace (Sponsored Links)