jschultzpedersen Posted May 30 Author Share Posted May 30 There is always room for improvement. So here is a more generic Basic version of a conversion program. It will convert a decimal number to any number system between 2-16. It can return an unsigned number of any size (if you set the length parameter to 0), while a signed number only works up to the limit of 2 to the n'th power-1, where n is the number system you convert to. You can pad the returned number with zeroes so the string, that is returned with the result has a certain length (excluding any minus sign). The parameters for the sub are: N The decimal number to be converted R The number system to convert to S The number of characters in the answer string. Use 0 to just return the actual length. SF The sign Flag. True means it returns the value in signed format. False returns the value as unsigned. A$ The string holding the result. 10 CALL CNVRT(34000,16,5,0,A$):: PRINT A$ :: A$="" ** Convert 34000 to HEX unsigned. Return string length is 5 11 CALL CNVRT(34000,16,5,-1,B$):: PRINT B$ ** Convert 34000 to HEX signed. Return string length is actual length. 12 CALL CNVRT(42,2,0,0,C$):: PRINT C$ ** Convert 42 to binary unsigned. Return string length is actual length. 20 END 1000 SUB CNVRT(N,R,S,SF,A$) 1010 FLAG=0 :: S$="0123456789ABCDEF" :: L=2^R 1020 IF NOT SF THEN 1060 1030 IF N>=L THEN A$="NUMBER TOO BIG" :: SUBEXIT 1040 IF NOT(N<(L/2))THEN FLAG=-1 1050 IF FLAG THEN N=L-N 1060 V=INT(N/R):: D=N-R*V 1070 A$=SEG$(S$,D+1,1)&A$ 1080 N=V :: IF V>0 THEN 1060 1090 IF S<>0 THEN A$=RPT$("0",S-LEN(A$))&A$ 1100 IF FLAG THEN A$="-"&A$ 1110 SUBEND 3 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.