I have an application where I am reading values from a text file on the NXT and I need to convert some of the read numbers from their character representation to numbers (short/int). For example;

sReadVal = "12";

What is the best way to convert this to a short of 12? Are there any string-to-number conversion functions available?

Thanks,Chris

Thu Feb 03, 2011 7:38 pm

MHTS

Guru

Joined: Sun Nov 15, 2009 5:46 amPosts: 1523

Re: Converting String to Short/Int

I don't have RobotC in front of me but I vaguely recall it may support sscanf. If not, check if it supports atoi. If not, you can always write your own

Thu Feb 03, 2011 9:24 pm

SeaLyon

Novice

Joined: Mon May 31, 2010 8:02 amPosts: 55

Re: Converting String to Short/Int

Hi,

I don't think RobotC has any of the conversion functions built-in. Certainly not atoi or sscanf. I've written my own for now, but if there are any built-in functions to make it easier, I'd like to know.

Thanks,Chris

Thu Feb 03, 2011 9:34 pm

brianchen11

Rookie

Joined: Tue Jul 03, 2012 5:16 amPosts: 43

Re: Converting String to Short/Int

There are. I've used atoi in my programs before. I believe sscanf works as well but I'm not sure how to use it.

Wed Jul 11, 2012 8:36 pm

JohnWatson

Site Admin

Joined: Thu May 24, 2012 12:15 pmPosts: 722

Re: Converting String to Short/Int

brianchen11 wrote:

There are. I've used atoi in my programs before. I believe sscanf works as well but I'm not sure how to use it.

^^This is correct. Setting a numeric variable (int, long, etc) using atoi and a string will convert the "12" to a numeric 12.

Who is online

Users browsing this forum: No registered users and 2 guests

You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum