Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Discussion / Converting Strings to Integer

Author
Message
Ben Johnson
18
Years of Service
User Offline
Joined: 6th Jul 2007
Location:
Posted: 6th Jul 2007 14:11
I am fairly new to DB, and I have run into the problem of needing to convert a String retrieved from a read file to a integer so i can compare it to another value. How would I do this?
LBFN
19
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 6th Jul 2007 15:13
Look at the asci codes of the letters in the string. An example:


LB
TDK
Retired Moderator
23
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 6th Jul 2007 16:42
I think LBFN misunderstood the question.

If the string read in is a number in string format then just use:

A$ = 'String Read In'
Number = VAL(A$)


TDK_Man

Libervurto
20
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 6th Jul 2007 19:09 Edited at: 6th Jul 2007 20:59
@ Ben

[edit]
be careful though, if your string starts with a letter then VAL(A$) will return 0. If this is likely to happend use ASC(A$) to check the ASCII codes to make sure you are reading numbers and not letters. if you're not sure what ASCII codes are look up ASC() in the help file index.

I make music and art. Here is a sample of my art: done in Paint! It's all I have
TDK
Retired Moderator
23
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 6th Jul 2007 20:23
Quote: "Do what LBFN said"


Why would you recommend someone ignore the correct information and follow something which is incorrect?

What LBFN suggests doing has nothing to do with what Ben actually asked for advice on as ASCII codes are unrelated to the actual value of a number held as a string, (I think he just mis-read the opening post).

Quote: "be careful though, if you have ANY letters in the string at all then VAL(A$) will return 0."


Not true. Even if the string contains numeric characters at the front and alpha characters on the end, VAL() will still work.



... will still print 123!

TDK_Man

Libervurto
20
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 6th Jul 2007 20:56
Quote: "
What LBFN suggests doing has nothing to do with what Ben actually asked for advice on as ASCII codes are unrelated to the actual value of a number held as a string, (I think he just mis-read the opening post).
"


Yeah I just meant use it to check the string for numbers.
As for the VAL() returning 0, [testing] Oh it does! sorry I didn't know that lol

Sorry TDK I was following on from what you said, I wasn't correcting you. I'll edit my post so it's correct.

I make music and art. Here is a sample of my art: done in Paint! It's all I have
LBFN
19
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 7th Jul 2007 01:13
@ Ben Johnson

I did mis-read your request.

However, if the first character in the string is not 0 - 9, val(string$) will return 0. Example:


If you are absolutely sure that an alpha character will never be in the data that is loaded, then just use val for the string. If you are unsure, you could use the asc function to determine that it is indeed a numeric value before you attempt to determine it's value.

LB
TDK
Retired Moderator
23
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 7th Jul 2007 01:23
No harm done - I've answered a different question to the one that's been asked many times before - and no doubt will do so again many times again in the future!

And LBFN is correct - VAL() does return zero with strings that are non-numeric at the start (not the end).

TDK_Man

Ben Johnson
18
Years of Service
User Offline
Joined: 6th Jul 2007
Location:
Posted: 7th Jul 2007 20:11
Thanks for the replies, since I am new and under moderation I found the answer while looking through the commands. I used the Val() command like suggested, thanks for the help.

Login to post a reply

Server time is: 2026-07-05 18:17:19
Your offset time is: 2026-07-05 18:17:19