Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Opposite of HEX$ in Qbasic?
#11
Quote:How about this? Big Grin
[syntax="qbasic"]
'Omni-base converter
'Peter O'Rourke (Marinedalek) 2004
DECLARE FUNCTION reverse$ (text$)
DECLARE FUNCTION baseconv$ (basefrom%, baseto%, numfrom$)
DIM SHARED digitarray AS STRING * 36
digitarray = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
CLS
PRINT baseconv$(16, 2, "FFFF")

FUNCTION baseconv$ (basefrom%, baseto%, numfrom$)
IF baseto% <= 1 OR basefrom% < 1 OR baseto% > 36 OR basefrom% > 36 THEN baseconv$ = "0": EXIT FUNCTION
FOR position% = LEN(numfrom$) TO 1 STEP -1
tempdec& = tempdec& + (basefrom% ^ (LEN(numfrom$) - position%)) * (INSTR(digitarray, MID$(numfrom$, position%, 1)) - 1)
NEXT position%
IF baseto% = 10 THEN
baseconv$ = LTRIM$(STR$(tempdec&)) 'skip last calculation if destination is
'decimal - it 'd be a waste of time.
ELSE
DO
tempdest$ = MID$(digitarray, (tempdec& MOD baseto%) + 1, 1) + tempdest$
tempdec& = tempdec& \ baseto%
LOOP UNTIL tempdec& = 0
baseconv$ = tempdest$
END IF
END FUNCTION

FUNCTION reverse$ (text$)
FOR i% = LEN(text$) TO 1 STEP -1
temptxt$ = temptxt$ + MID$(text$, i%, 1)
NEXT i%
reverse$ = temptxt$
END FUNCTION

[/syntax]

Any feedback?
Yes, some feedback on your coding style:
Code:
DIM SHARED digitarray AS STRING * 36
digitarray = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Why the fixed length string? What's wrong with just plain
Code:
dim shared digitarray$
digitarray$ = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
On the other hand your code is full of explicit parameter type assignments like %, &. Example:
Code:
tempdec& = tempdec& \ baseto%
This makes code error prone and also hard to read. Define your variable types up fron in the program with DIMs.

You got all the latest color goodies, but your coding structure regarding indentation is not good. Example:
Code:
FOR i% = LEN(text$) TO 1 STEP -1
temptxt$ = temptxt$ + MID$(text$, i%, 1)
NEXT i%

As far as the technical merit of your code, I'll need to test it when I have time.
*****
Reply
#12
Maybe he has a purpose for shared variables? Who knows. Big Grin Also, there's nothing wrong with fixed-length strings...everyone codes differently, thankfully there's no real "right or wrong" coding style as long as the job gets done. Big Grin
I'd knock on wood, but my desk is particle board.
Reply
#13
Quote:Maybe he has a purpose for shared variables? Who knows. Big Grin Also, there's nothing wrong with fixed-length strings...everyone codes differently, thankfully there's no real "right or wrong" coding style as long as the job gets done. Big Grin
You read my post before I was finished editing it. I removed the comment about SHARED when I realized that the shared variable was used inside of a function. Actually it should have been set up as a CONST which would automatically make is shared.

I personnally never use fixed length strings because the variable name without the trailing $ can be confused with a numeric variable, plus the fact that I find no real "need" to use them.

Yes, personal coding styles are fine, as long as they make things clear for the next programmer who takes over the program. Nobody pays much attention to this "next programmer" issue. I spent 7 years at McGraw-Hill where about 70% of my time was spent fixing and retrofitting existing PickBasic programs of a huge ERP system called CISPUB that they had purchased just before I got there. After this experience, you appreciate clear, understandable code.
*****
Reply
#14
hmm.. don't know how that reversal routine found it's way in there... Normally I do indent the contents of FOR-NEXT loops, I must have forgotten. Here's a version that conforms to your "style" Big Grin (I have kept all the veriables passed through to the function complete with their trailing %. It'll be a cold day in hell before I stop :wink: .

[syntax="qbasic"] 'Omni-base converter v0.2
'Peter O'Rourke (Marinedalek) 2004
DECLARE FUNCTION baseconv$ (basefrom%, baseto%, numfrom$)
DIM tempdec AS LONG

CONST digitarray$ = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
CLS
PRINT baseconv$(16, 2, "FFFF")

FUNCTION baseconv$ (basefrom%, baseto%, numfrom$)

'Next line exits the sub if an invalid base is given
IF baseto% <= 1 OR basefrom% < 1 OR baseto% > 36 OR basefrom% > 36 THEN baseconv$ = "0": EXIT FUNCTION

'This FOR-NEXT loop converts numfrom$ into decimal for conversion to the specified base
FOR position% = LEN(numfrom$) TO 1 STEP -1
tempdec = tempdec + (basefrom% ^ (LEN(numfrom$) - position%)) * (INSTR(digitarray$, MID$(numfrom$, position%, 1)) - 1)
NEXT position%

IF baseto% = 10 THEN
baseconv$ = LTRIM$(STR$(tempdec)) 'skip last calculation if destination is
'decimal - it'd be a waste of time.
ELSE
'This section converts the decimal number into the specified base
DO
tempdest$ = MID$(digitarray$, (tempdec MOD baseto%) + 1, 1) + tempdest$
tempdec = tempdec \ baseto%
LOOP UNTIL tempdec = 0
baseconv$ = tempdest$
END IF
END FUNCTION

[/syntax]
8% of the teenage population smokes or has smoked pot. If you're one of the 2% who hasn't, copy and paste this in your signature.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)