Jump to content
Sign in to follow this  
logix1390

Converting integer to string atoi

Recommended Posts

Hello. I am trying to get the integer value from my rbd piece names "piece0", piece1", piece3", etc . and convert that into a string

I am trying to do this based on what the help card has said:

int  atoi(string str)

i@nameInt= atoi(s@name);

Am I not doing this correctly? this will return 0 

Any help would be appreciated. thank you. 

atoi_int_to_string.hipnc

Share this post


Link to post
Share on other sites
Posted (edited)

You have to parse the string, of course. Fortunately, your piece name all have the same length prefix, so you can get away with an array manipulation, using a constant.

i@nameInt= atoi(s@name[5:]);

https://www.sidefx.com/docs/houdini/vex/strings.html

Edited by Atom

Share this post


Link to post
Share on other sites

@Atom Thank you for the response Atom. That makes sense now. I can't help but ask a few follow up questions.  

So as you mentioned, my name attribute in this case has the same length prefix which is making it easy to isolate the integer for the atoi function.

So if I had a different name attribute with a longer length prefix then it will return 0 if I were to use the same expression .

My question is , how would I automatically detect the length of the name prefix to always isolate the integer at the end ?

My first thought would be to find the length of the string using the len function..

i@mylen=len(@name);
i@nameInt= atoi(s@name[@mylen:]);

But this of course wont work because that will find the length of the string including the integers. 

Is there anything else I should be looking into to achieve this ?

Thank you

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×