Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
String to integer
#1
Is there anyway to turn a string into a integer it would be really helpfull
his world has been connected...
Tied to the darkness.
Soon to be completely eclipsed.
There is so very much to learn...
You understand so little.
A meaningless effort.
One who knows nothing can understand nothing.
-Ansem Bringer of darkness and creator of the heartless
Reply
#2
a% = VAL(s$)
Reply
#3
and from an integer to a string? a$ = str$(b%) :lol:
Reply
#4
:wink: And both points togetter:

[syntax="QBasic"]
stringy$ = "123456789"
'String > Integer
number% = VAL(stringy$)
'Integer > String
txtnum$ = STR$(number%)
[/syntax]

^_^
Kevin (x.t.r.GRAPHICS)

[Image: 11895-r.png]
Reply
#5
Quote:
Code:
stringy$ = "123456789"
'String > Integer
number% = VAL(stringy$)

You won't get the same answer (if you even know to pass the overflow error), because the number 123456789 won't ever fit in an integer in QB Wink

^_^
Reply
#6
True. But if you DIM it as LONG it will.

>anarky - Standard length.
Screwing with your reality since 1998.
Reply
#7
There are some additional considerations regarding the results produced by STR$ when using QB.

STR$(number)
1) If number is positive, the string will have a leading space.
2) If number is negative, the string will have a leading minus sign.
3) In either case, the string will have a trailing space. This is a "feature" that is rarely documented.

As a matter of fact, the 3 conditions above also apply to any number variable printed to the screen or to an output file.

Test it.
*****
Reply
#8
yeah, and i discovered these "features" while programming GUIs. It's always good to "trim" the strings.
Reply
#9
Well, if the string is from a file you wrote with a guaranteed legal value, then k%=val(S$) is good. But if it is a value supplied by the user, you need to check for validity unless you are the only user or you know only skilled QBasic programmers are supplying the input. Otherwise your program will bomb or give strange results.

Mac
Code:
DECLARE FUNCTION Valid% (S$)
CLS
PRINT "Enter strings to be converted to integer"
PRINT "(Just press Enter when finished)"
PRINT
PRINT "Note: Enter integers like -84, 345, ... not 4E2, etc."
DO
  PRINT
  LINE INPUT "String: "; S$
  IF S$ = "" THEN EXIT DO
  IF Valid(S$) THEN
    k% = VAL(S$)
    PRINT "You entered "; k%
  ELSE
    PRINT "Invalid integer"
  END IF
LOOP
CLS
SYSTEM

FUNCTION Valid% (S$)
'Valid integer range: -32768 to 32767
Valid% = 0
w$ = LTRIM$(RTRIM$(S$))
IF w$ = "" THEN EXIT FUNCTION
IF w$ = "-" THEN EXIT FUNCTION
IF LEFT$(w$, 1) = "-" THEN sign = -1: w$ = MID$(w$, 2) ELSE sign = 0
IF LEN(w$) > 5 THEN EXIT FUNCTION
FOR i = 1 TO LEN(w$)
  IF INSTR("0123456789", MID$(w$, i, 1)) = 0 THEN EXIT FUNCTION
NEXT i
IF LEN(w$) < 5 THEN Valid% = -1: EXIT FUNCTION
SELECT CASE VAL(LEFT$(w$, 4))
CASE IS < 3276: Valid% = -1: EXIT FUNCTION
CASE IS > 3276: EXIT FUNCTION
END SELECT
SELECT CASE VAL(RIGHT$(w$, 1))
CASE IS < 8: Valid% = -1: EXIT FUNCTION
CASE IS > 8: EXIT FUNCTION
END SELECT
Valid% = sign
END FUNCTION
Reply
#10
Quote:There are some additional considerations regarding the results produced by STR$ when using QB.

STR$(number)
1) If number is positive, the string will have a leading space.
2) If number is negative, the string will have a leading minus sign.
3) In either case, the string will have a trailing space. This is a "feature" that is rarely documented.

As a matter of fact, the 3 conditions above also apply to any number variable printed to the screen or to an output file.
FORGIVE ME. I DIDN'T TEST ALL THE COMBINATIONS.
Condition 3) above only applies when you output a number to the screen or output file. That is, if you do:
X$=STR$(NUMBER)
The X$ will not have a trailing space.

Here are some additional tests I did. The comments indicate whether or not the output has a trailing space.
Code:
open "xxx" for output as #2
n=12
x=-99
print #2,n               'TRAILING SPACE
print #2,x               'TRAILING SPACE
print #2,"ZZZ"        'This is a delimeter to see any trailing blanks.

z$="25"
v=val(z$)
print #2,v               'TRAILING SPACE
print #2,"ZZZ"

n=34
z$=str$(n)
print #2,z$             'NO TRAILING SPACE
print #2,"ZZZ"

print #2,str$(n)      'NO TRAILING SPACE
print #2,"ZZZ"
Hope this helps clear up the issue.
*****
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)