string 2 integer

Anything that doesn't fit elsewhere.
Post Reply
Tsirogiannis
Posts: 9
Joined: Mon May 29, 2006 12:33 pm
Location: National Technical University of Athens

string 2 integer

Post 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
Raj
Posts: 220
Joined: Thu Jun 09, 2005 1:09 pm
Location: Groningen, The Netherlands
Contact:

Post 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.
ted
Site Admin
Posts: 6305
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Post 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."
Tsirogiannis
Posts: 9
Joined: Mon May 29, 2006 12:33 pm
Location: National Technical University of Athens

Post by Tsirogiannis »

Thanks a lot for the tip Raj.
And I am sorry for the wrong forum.
Cheers
ted
Site Admin
Posts: 6305
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Post 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."
Post Reply