element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Parsing integer strings?
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Suggested Answer
  • Replies 19 replies
  • Answers 2 answers
  • Subscribers 393 subscribers
  • Views 2373 views
  • Users 0 members are here
  • serial_communications
  • potentiometers
  • parsing
  • boolean
  • arduino
Related

Parsing integer strings?

scorpion
scorpion over 9 years ago

I need help with parsing a string of integers sent from another Arduino. The values will be like this 1, 0, 1234, 1234,

1 is a Boolean

0 is a Boolean

1234 are pot values

I would like for it to go to variables

Vpot is first pot value

Hpot is second pot value

C is first Boolean

Z is second Boolean

 

Thanks,

Ben Bonen

  • Sign in to reply
  • Cancel

Top Replies

  • shabaz
    shabaz over 9 years ago +1
    sscanf is your friend, probably supported by Arduino (I don't use Arduino). Reddit on the topic however is here. https://www.reddit.com/r/arduino/comments/pznnj/using_sscanf_with_arduino/
  • gadget.iom
    gadget.iom over 9 years ago +1
    The Serial.readStringUntil command may help you if you are receiving the data through the serial port. Something like this might work: void loop(){ String C = Serial.readStringUntil(',').toInt(); Serial…
  • clem57
    clem57 over 9 years ago +1
    One way is using indexOf( ) like in http://stackoverflow.com/questions/11068450/arduino-c-language-parsing-string-with-delimiter-input-through-serial-interf… Then easy to convert to known values. Suggest…
Parents
  • balearicdynamics
    0 balearicdynamics over 9 years ago

    Hi Ben,

     

    please find below the method I have used in several string parsers with the Arduino boards. This will work for sure.

     

    Enrico

     

    /** \fn int charsToInt(int startChar, int numChars)
        \brief Convert a string from the command to integer
    
    
        \param startChar initial character in the command string
        \param numChars number of characters composing the number
        \return The converted interger value
    */
    int charsToInt(int startChar, int numChars)
    {
        char * value_to_convert;
        String temp = String(6);
        int i;
    
    
        for (i = 0; i < numChars; i++)
            temp += cmd.cmdData[i + startChar];
    
    
        value_to_convert = temp;
        i = atoi(temp);
    
    
        return i;
    }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • scorpion
    0 scorpion over 9 years ago in reply to balearicdynamics

    @Enrico Miglino

    Thanks!

    Will this send the values to different variables?

    Ben

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • balearicdynamics
    0 balearicdynamics over 9 years ago in reply to scorpion

    Hi Ben,

     

    as this is an independent function, you can call it passing the string you want to parse and use the integer return value as well. About the error correction it is true, there is no error checking and - by my point of view - there is a reason. As this function is used in a parser; I mean, a string parser processing multiple kind of commands with or without parameters, command separators etc. when the conversion is called all the syntax check are already done.

     

    In your case I suggest you check before that the string you send is a valid numeric string that - by a pragmatic point of view - is something independent by the conversion function. In this moment I don't remember what kind of result aoti() call returns when a non-numeric character is passed.

     

    Enrico

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Reply
  • balearicdynamics
    0 balearicdynamics over 9 years ago in reply to scorpion

    Hi Ben,

     

    as this is an independent function, you can call it passing the string you want to parse and use the integer return value as well. About the error correction it is true, there is no error checking and - by my point of view - there is a reason. As this function is used in a parser; I mean, a string parser processing multiple kind of commands with or without parameters, command separators etc. when the conversion is called all the syntax check are already done.

     

    In your case I suggest you check before that the string you send is a valid numeric string that - by a pragmatic point of view - is something independent by the conversion function. In this moment I don't remember what kind of result aoti() call returns when a non-numeric character is passed.

     

    Enrico

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Children
No Data
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube