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
  • gadget.iom
    0 gadget.iom over 9 years ago in reply to balearicdynamics

    That might be a bit difficult to implement where the value of vpot falls below 1000.

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

    Hi Paul,

     

    what do you mean with vpot ?

    (maybe the method should be generalised a bit more).

     

    Enrico

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

    My interpretation of his question is that he is trying to break the string into four variables.

    So

    1, 0, 1234, 1234,

    would be put into variables

    C, Z, Vpot, Hpot

     

    When using code that defines start and length positions you would extract the final value (Hpot) by looking at a starting point of 12 and a length of 4.

    In the example string supplied above this would be sufficient to get the value 1234.

     

    The issue with position based referencing is that then the third part of the string fell below 1000 e.g.:

    1, 0, 993, 1234,

    the final variable would be missing the first digit and only take the remaining 4 digits "234,"

    • 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 gadget.iom

    @Paul Ellison

    That is true, but that number (1234) was just and example and I am not sure what the actual analog range of a pot is.

    So, what if I mapped the input value to a range of 0 to 999 and used that?

    Would that work?

     

    Ben

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

    @Paul Ellison

    That is true, but that number (1234) was just and example and I am not sure what the actual analog range of a pot is.

    So, what if I mapped the input value to a range of 0 to 999 and used that?

    Would that work?

     

    Ben

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Children
  • Robert Peter Oakes
    0 Robert Peter Oakes over 9 years ago in reply to scorpion

    See below for my other response using strtok

     

    strtok will not care how big the number is, it looks for the delimeters and effectivly splits up the string based on them

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

    The code I've provided below might just work regardless of with the pot value runs into 0, 1, 2, 3 or 4 digits. Would you mind giving it a try.

    Would be interested to know if it works image

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • neilk
    0 neilk over 9 years ago in reply to gadget.iom

    Paul, did you forget to include the code? I can't see it.

     

    Neil

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • gadget.iom
    0 gadget.iom over 9 years ago in reply to neilk

    Hi neilk

    It was posted earlier. Can be found by scrolling down to here:

    Re: Parsing integer strings?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • neilk
    0 neilk over 9 years ago in reply to gadget.iom

    Hi gadget.iom

     

    Thanks Paul, got it now.

     

    Neil

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • gadget.iom
    0 gadget.iom over 9 years ago in reply to neilk

    You're welcome. Glad it helps somebody. image

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
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