Page 1 of 1 1
Topic Options
#27349 - 2002-08-19 03:23 PM Convert Variable from String (8) to Integer (2)
Svein Arild Haugum Offline
Fresh Scripter

Registered: 2002-02-13
Posts: 15
Loc: Oslo, Norway
This might be a stupid/easy question, but I'll give it a go anyway....

My problem is:

I use ReadProfileString to read a value from a INI file.

This makes the variable a String Value (8). The value here is always a number between 1000 and 9999.

To make something like "$Val = $Val + "1"" work, i need to convert the value to a Integer.

This workes nice if i use something like "$Val = 1000" but not by using something like "$DP = ReadProfileString("filename","section","key")"

Any takers?

Top
#27350 - 2002-08-19 03:26 PM Re: Convert Variable from String (8) to Integer (2)
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
code:
$DP = VAL(ReadProfileString("filename","section","key"))

That's really all you need.

Top
#27351 - 2002-08-19 03:26 PM Re: Convert Variable from String (8) to Integer (2)
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
There are two ways to do this actually:

$string = readprofilestring(...)

$sum = val($string) + val($string)

-or-

$sum = 0 + $string + $string

the second one is odd, but because the expression starts with a number, the strings get converted into numbers.

Top
#27352 - 2002-08-19 03:42 PM Re: Convert Variable from String (8) to Integer (2)
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
A bit more info...

Variables (and expressions) in KiXtart are very flexible when it comes to typing. Basically the left hand element in any expression coerces the expression type. Be careful about operator precedence though.
For instance,
code:
$i="A" + 5 * "3"

Will work, and the result will be "A15"
code:
$i="A" + 5 + "3"

Will work, and the result will the "A53"

In the first example, the '*' operator has the highest precedence so '5 * "3"' is evaluated first. Because the '5' on the lefthand side of the equation is numeric the string '"3"' is converted to a numeric and the multiplication succeeds with the result '15'. Now the expression becomes '"A" + 15'. The left hand side of the expression is now a string, so the '15' is converted to a string. This causes the '+' to operate as a catenation operator, so the result is 'A15'.

In the second example the two '+' have the same precedence, so are evaluated left-to-right.
The string '"A"' causes the '5' to be converted to a string, so the operation is a catenation resulting in the string '"A5"'. The next expression becomes '"A5" + "3"', which again is a string expression, so the result is another catenation with the result '"A53"'

Top
#27353 - 2002-08-19 04:02 PM Re: Convert Variable from String (8) to Integer (2)
Svein Arild Haugum Offline
Fresh Scripter

Registered: 2002-02-13
Posts: 15
Loc: Oslo, Norway
Well, this is what seems to work..

$DP = ReadProfileString("U:\Dp_ini\dp.ini","DP","Current")
; Reads the value, makes is a string value (8)

$DP_TMP = 0 + $DP + $DP - $DP
; converts the value to long integer (3). You can see i put on a "- $dp" there, that is becouse the proposed solution doubled the value...

$DP_NY = $DP_TMP + "1"
; increases by one.

WriteProfileString("U:\Dp_ini\dp.ini","DP","Current","$DP_NY")
WriteProfileString("W:\WINDOWS\DPTWS4W.INI","TCP/IP","PORT","$DP_NY")
;sets the value on the refrence file, and the current ini file. this is so no users get the same value in theyr ini file for at least 8999 total logons...

messagebox($dp_ny,"woha")
vartype( $dp_ny )
; just for testing...

anything i have go completely wrong?

Top
#27354 - 2002-08-19 04:06 PM Re: Convert Variable from String (8) to Integer (2)
Svein Arild Haugum Offline
Fresh Scripter

Registered: 2002-02-13
Posts: 15
Loc: Oslo, Norway
I should add i have not yet made the reset function wich i will need at the value 9999, so all i posted in the last post, is not gonna work yet...
Top
#27355 - 2002-08-19 04:10 PM Re: Convert Variable from String (8) to Integer (2)
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
You should definitely use VAL() to convert a string to a value like
code:
$DP = VAL(ReadProfileString("U:\Dp_ini\dp.ini","DP","Current"))

Also, if you are using KiXtart 4.10 or later, please read the KiXtart manual for the following functions: CDBL(), CINT(), CSTR()
_________________________
There are two types of vessels, submarines and targets.

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 2870 anonymous users online.
Newest Members
fgarcia, Viginette, ManuvdWielNL, Sir_Barrington, batdk82
17889 Registered Users

Generated in 0.058 seconds in which 0.026 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org