Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to convert any passed variable into a string
#1
I need a function to convert any passed variable into a string, even if it is a string. It's for StatLib, so helpers go in the credits if you want (like that's any incentive :wink: ). So far I've tried writing the variable to a file and getting it back as a string, but that doesn't work. Any help?
Reply
#2
Just write the variable to a file, and then read as a string.
Code:
x = 512
s$ = "String"
open "test" for output as #1
print #1, x
print #1, s$
close

open "test" for input as #1
input #1, a$
input #1, b$
close

print a$, b$

Both variables are now strings. Or you could just use STR$() and check for errors.
Reply
#3
:|
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
Reply
#4
I'll just post this before someone yells "rtfm". Like RST said: http://qbasicnews.com/qboho/qckstr$.shtml

or the alternative
Code:
x = 410
do until x = 0
  n$ = chr$(48 + (x mod 10)) + n$
  x = x \ 10
loop
print n$

but str$'s easier. Also, if you use it on a postive number it puts a blank space in the beginning, so remember to use ltrim$(str$(num))
i]"I know what you're thinking. Did he fire six shots or only five? Well, to tell you the truth, in all this excitement, I've kinda lost track myself. But being as this is a .44 Magnum ... you've got to ask yourself one question: 'Do I feel lucky?' Well, do ya punk?"[/i] - Dirty Harry
Reply
#5
I know about the STR$() function, it's imperative to StatLib, where all calculations are based on strings.

I already have a program like what Mr RST made. It's just that I needed a function that could accept any type of variable, regardless of being a number or string, and convert it to a string. I now realise I can check to see if it is a string with error handling, but that doesn't solve the problem of numbers being converted to single precision when the function is called:

Code:
FUNCTION convert$(lala)
open file...
put variable...
reopen: get variable...
END FUNCTION

To put it really basically (and to save some hassle): Is there a way to determine the type of a variable using QBasic commands?
Reply
#6
to be able to pass a variable to a function and not have the function care what type of variable it is in the first place. I don't think *that's* possible. You might be able to get around that by passing a user-defined variable. However, that would require user-defined variables to allow their type to be changed dynamically, depending on what you wanted your function to operate on. I don't think *that* is possible either. However, you could have a user-defined type with elements in it for all of QB's intrinsic types and pass the user-defined variable to the function along with a flag so the function knows which element to operate on.
ravelling Curmudgeon
(geocities sites require copying and pasting URLs.)
I liked spam better when it was something that came in a can.
Windows should be defenestrated.
Reply
#7
Hummmm... I'll see about it when I've finished my bio assignment in *gulp* windows...
Reply
#8
I suppose you could just create multiple functions. One for each data type you need to convert. maybe? :normal:

(Without meaning to sound like an anus) you could always adapt your program to anticipate what data type needs to be converted into a string. e.g. what the result of a calculation, or user input will be.
url=http://www.spreadfirefox.com/?q=affiliates&id=60131&t=79][Image: safer.gif][/url]
END OF LINE.
Reply
#9
From last version of my jpeg viewer...
Code:
'An useful trick from Ethan Winer's book, to use PRINT USING to format a string
frmt$=" /           /     #####   ####    ####   ###"
jfit$="hello"
md&=&hffff
display.yres=640
display.xres=800
display.bpp$=chr$(16)
    'dummy file must be opened and closed for each new string or the trick won't work
    NUL = FREEFILE: OPEN "\DEV\NUL" FOR RANDOM AS #NUL LEN = 255
    PRINT #NUL, USING frmt$; jfit$; HEX$(md&); display.xres; display.yres; ASC(display.bpp$)
    LINE INPUT #NUL, B$
    print  B$
    CLOSE NUL
Antoni
Reply
#10
do you know that for sure because it is wrong.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)