How to convert string to int?

Once a string is a string there's no going back. You need to rework the logic elsewhere instead. What are you trying to do?

DC
 
Well that was fun so the only restriction of this function is you can not start your input number with a "0" well you can but it will be ignored. I have also made sure the code will ignore any non number characters enjoy.

Code:
 void main()
 {
	 char a = "123";
	 int b = string2int(a)+100;
 settextobj(1, 140 , 90 , 3, 99999999999999, "output "+b);
 }


char string2int(char string)
{//msmalik681 convert string to intiger needs extra fuction "merge".
	int i;
	char tempCa;
	char tempCb;
	int tempI;
	for(i=0;i<strlength(string);i++)
	{
		if(i==0)
		{
		tempCb = strleft(string,i+1);
		} else { 
		tempCa = strleft(string,i+1);	
		tempCb = strright(tempCa,i);
		}
		tempI = NULL();
		
		if(tempCb=="9"){
		tempI = 9;
		} else if(tempCb=="8"){
		tempI = 8;
		} else if(tempCb=="7"){
		tempI = 7;
		} else if(tempCb=="6"){
		tempI = 6;
		} else if(tempCb=="5"){
		tempI = 5;
		} else if(tempCb=="4"){
		tempI = 4;
		} else if(tempCb=="3"){
		tempI = 3;
		} else if(tempCb=="2"){
		tempI = 2;
		} else if(tempCb=="1"){
		tempI = 1;
		} else if(tempCb=="0"){
		tempI = 0;
		}
		
		if(tempI != NULL())
		{
			setlocalvar("string."+i+"",tempI);
		}	
	}
tempI = NULL();
for(i=0;i<strlength(string);i++)
	{
		if(getlocalvar("string."+i+"") != NULL())
			{
				if(tempI==NULL())
				{
					tempI = getlocalvar("string."+i+"");
					continue;
				} else {
					tempI = merge(tempI,getlocalvar("string."+i+""));
				}
			}
			
	}

return tempI;
}
 
int merge(int int1, int int2)
{//msmalik681 merge 2 intiger values togeather.
	if(int1 != NULL() && int2 != NULL())
	{
		int int2_copy = int2;
		do
		{
			int1 *= 10;
			int2_copy /= 10;
		} while (int2_copy);
	
	return int1 + int2;
	} else if(int1 != NULL()) {
		return int1;
	} else if(int2 != NULL()){
		return int2;
	} else { return 0;}
    
}
 

Attachments

  • test.jpg
    test.jpg
    163.4 KB · Views: 12
I mean no disrespect at all msmalik681, but that just about the worst way you could solve this problem. When I said you can't go back from a string, I meant it. All you're doing here is a long series of very slow brute force search and outputs.

SimonSmith It would really be best to tell us how you got the string of numbers in the first place, and then we can come up with a more tenable fix.

DC

 
Damon Caskey ouch that was a bit harsh.

I get where your coming from. At the point the string number is made also store it as a integer.  That would be a simpler solution but it is not the direct question that was asked.
 
Damon Caskey said:
Once a string is a string there's no going back. You need to rework the logic elsewhere instead. What are you trying to do?

DC
Thank you .
Just curious about that.
I'll try to avoid this.

 
SimonSmith  DC was asking how you got a int stuck in a char so a better solution could be devised or where you just asking as a general question ?
 
msmalik681 said:
SimonSmith  DC was asking how you got a int stuck in a char so a better solution could be devised or where you just asking as a general question ?
For example, my idea is like this:

void branch = openborvariant("branch_name");
changeentityproperty(ent,"maxhealth",branch*10+100);

If I can convert "branch" into an integer, the result will be correct.
 
Do you use branch names in any other script if not there is a option to count how many characters used in a string.  Otherwise just use a if statement to convert each string into desired integer.
 
Back
Top Bottom