Welcome to Telelogic Product Support
  Home Downloads Knowledgebase Case Tracking Licensing Help Telelogic Passport
Telelogic DOORS (steve huntington)
Decrease font size
Increase font size
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
Search Topic Search Topic
Topic Tools Topic Tools
Quick Reply Quick Reply
Subscribe to this topic Subscribe to this topic
E-mail this topic to someone. E-mail this topic
Bookmark this topic Bookmark this topic
View similar topics View similar topics
View topic in raw text format. Print this topic.
Answer This question was answered by Peter Albert, on Wednesday, August 27, 2008 7:24 PM

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
 27-Aug-2008 01:53
User is offline View Users Profile Print this message


Gyan Penrose-Kafka

Posts: 7
Joined: 31-Jul-2008

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.
Report this to a Moderator Report this to a Moderator
 27-Aug-2008 08:04
User is offline View Users Profile Print this message


Peter Albert

Posts: 232
Joined: 30-Dec-2005

Answer 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
Report this to a Moderator Report this to a Moderator
 27-Aug-2008 09:04
User is offline View Users Profile Print this message


Kiran Chaturvedula

Posts: 14
Joined: 20-Apr-2008

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
Report this to a Moderator Report this to a Moderator
 27-Aug-2008 10:38
User is offline View Users Profile Print this message


Paul Howstan

Posts: 47
Joined: 27-Sep-2006

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
Report this to a Moderator Report this to a Moderator
 11-Sep-2008 19:23
User is offline View Users Profile Print this message


David Jakad

Posts: 94
Joined: 20-Jul-2007

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...
Report this to a Moderator Report this to a Moderator
Statistics
20925 users are registered to the Telelogic DOORS forum.
There are currently 1 users logged in.
The most users ever online was 15 on 15-Jan-2009 at 16:36.
There are currently 0 guests browsing this forum, which makes a total of 1 users using this forum.
You have posted 0 messages to this forum. 0 overall.

FuseTalk Standard Edition v3.2 - © 1999-2009 FuseTalk Inc. All rights reserved.