Discussion:
Ascii to Integer ?
(too old to reply)
Radical NetSurfer
2005-02-20 00:33:00 UTC
Permalink
I am trying to obtain the Integer value of the
first series of Digits (left-most characters)
found in a string...

examples:

"1abc" We need to know the base for this to work right.
"123xyz"
"15,..." Basic and C have no problems with this one!
"100 54 7" Basic and C have no problems with this one!

In each case, I need a routine smart enough to
be able to return:

1 (as integer)
123 (as integer)
15 (as integet)

if Val(Str, intVar, errpos) is used, then
all I ever get is
intVar == 0
and
errpos = 1,2,3, etc...

INSTEAD of simply returning a VALID integer
value up to first invalid character found, just like
Qbasic, VBdos, and 'C' would do normally !!

Another factor here that I've noticed:

C offers the means to set the Base of the value
to be parsed from a string, I see no such function
offered in the TP/BP or FreePascal default supplied
library.... so how is that done?

where's sscanf() when you need it...
where's atoi() when you need it....
where's strtol() when you need it...
Norm Mann
2005-02-20 04:08:24 UTC
Permalink
Post by Radical NetSurfer
I am trying to obtain the Integer value of the
first series of Digits (left-most characters)
found in a string...
"1abc" We need to know the base for this to work right.
"123xyz"
"15,..." Basic and C have no problems with this one!
"100 54 7" Basic and C have no problems with this one!
In each case, I need a routine smart enough to
1 (as integer)
123 (as integer)
15 (as integet)
is the fourth supposed to be 100? (as integer, not integet ;>)
Post by Radical NetSurfer
if Val(Str, intVar, errpos) is used, then
all I ever get is
intVar == 0
and
errpos = 1,2,3, etc...
yes, that's what it's supposed to do, according to my documentation. you
can use the copy procedure to extract the integer portion.
i.e.:

Strint := copy(Str, 1, errpos-1)

then use the Val procedure on Strint. I leave the actual conditional
statements to adjust the program flow for you to do.

Note that there are some limitations on the type of integer Val will
convert. If you need hex numbers, you may have to build you own.
Post by Radical NetSurfer
INSTEAD of simply returning a VALID integer
value up to first invalid character found, just like
Qbasic, VBdos, and 'C' would do normally !!
see below sscanf()
Post by Radical NetSurfer
C offers the means to set the Base of the value
to be parsed from a string, I see no such function
offered in the TP/BP or FreePascal default supplied
library.... so how is that done?
don't forget that Pascal was introduced in 1970, the other languages had the
advantage of hindsight and for a long time, Pascal was thought of as a
"teaching language" for structured programming. (I don't really recall hex
being commonly used before 1974 or so, before that, binary or octal was used
most of the time.)
Post by Radical NetSurfer
where's sscanf() when you need it...
when reading in data as an integer, the input stops at the first non-numeric
character, so the conversion is done already.
Post by Radical NetSurfer
where's atoi() when you need it....
where's strtol() when you need it...
anyway HTH

-NM

Loading...