Page 1 of 1

string 2 integer

Posted: Wed Sep 06, 2006 11:11 am
by Tsirogiannis
Hello.

I'm trying to convert a string into an integer in hoc code. I haven't found anything particularly useful in the built-in functions (is there really anything?) and I have been trying to use external functions, such as the atoi c function or the java.lang.Integer java class. But I think that I'm missing something in the way that I'm using these.

I will greatly appreciate any help.

Thanks

Posted: Wed Sep 06, 2006 12:41 pm
by Raj
Sscanf is the function you are looking for. See the example below (taken from the quick reference) which reads integers given in hex, binary and decimal format. Full documentation can be found in the quick reference: http://www.neuron.yale.edu/neuron/stati ... tml#sscanf.

Code: Select all

strdef s
double x[20]


sscanf("this is a test\n", "%*s%s", s)
sscanf("0xff 010 25", "%i%i%i", &x[0], &x[1], &x[2])
print x[0], x[1], x[2]
This question however belongs I think in the "Getting Started" or "Other Questions" forum.

Posted: Wed Sep 06, 2006 2:48 pm
by ted
Excellent answer, Raj, and you're also right about where this discussion thread should be.
I have already moved it to "Other questions."

Posted: Thu Sep 07, 2006 7:18 am
by Tsirogiannis
Thanks a lot for the tip Raj.
And I am sorry for the wrong forum.
Cheers

Posted: Thu Sep 07, 2006 10:16 am
by ted
Tsirogiannis wrote:I am sorry for the wrong forum.
No need to apologize--sometimes it's difficult to know exactly where to post a question.
hoc hacks is meant for stunningly brilliant bits of code, the kind of stuff you read and say
"I wish I had written that."