![]() |
Telelogic DOORS (steve huntington) | ![]() |
new topic :
profile :
search :
help :
dashboard :
calendar :
home
|
||
Latest News:
|
|
Topic Title: Casting a string to int? Topic Summary: Is there a way to turn the string "3" into the int 3? Created On: 27-Aug-2008 01:53 Status: Post and Reply |
Linear : Threading : Single : Branch |
![]() |
![]()
|
![]() Answer: Hy Gyan, try adding int targetint if (isValidInt(targetstring)) { targetint = intOf targetstring print targetint } else {print "'" targetstring "' is no integer value '"} to your script. Peter | |
![]() |
|
I have a substring that I extract from a larger string. eg:
string s = "3-My string" string sub = "-" int offset = null int length = null string targetstring findPlainText (s, sub, offset, length, false) int x = offset - length targetstring = s[0:x] So, targetstring is the string "3" I want to convert targetstring into an int, in this case to the integer value 3 so that I can do some math with it. I've searched and can't find a way to do this. Any help will be appreciated. |
|
![]() |
|
![]() |
|
Hy Gyan,
try adding int targetint if (isValidInt(targetstring)) { targetint = intOf targetstring print targetint } else {print "'" targetstring "' is no integer value '"} to your script. Peter |
|
![]() |
|
![]() |
|
One way to caste a string to an int is to use arrays and utilize get( ).
Array a = create(1,1) string str = "3" int i =1 put(a, 3, 1, 1) i = (int get(a, 1, 1)) // cast get as int print "i = " i This will print i = 3 |
|
![]() |
|
![]() |
|
Hi Kiran,
In your example above, the array returns an int because an int is put into it. I think the line put(a, 3, 1, 1) should be put(a, str, 1, 1). Also, you need double quotes after the print "i = " i line to convert the int back to a string. In theory, a string is represented in memory using ASCII. This means that the string "3" is captured as the number 51 if you read it out as an integer. See http://www.asciitable.com/ for a list of all of the ASCII codes. On saying that, in DXL int is represented by at least 32 bits, whereas a single ascii character is only 8 bits. This meas that only the least significant 8 bits will represent the ascii number and the other 24 bits are an undetermined value. Hence, a straight cast can not convert "3" into 3. Regards, Paul Edited: 27-Aug-2008 at 13:33 by Paul Howstan |
|
![]() |
|
![]() |
|
I wish I had known it was that easy. Here is a function I wrote to convert any string into an integer. It works based on the ASCII codes that Paul mentions...
|
|
![]() |
Telelogic DOORS
» DXL Exchange
»
Casting a string to int?
|
![]() |
FuseTalk Standard Edition v3.2 - © 1999-2009 FuseTalk Inc. All rights reserved.