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
Community Hub
Community Hub
Member's Forum Converting formatted string to struct values
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Leaderboard
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Community Hub to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 3 replies
  • Subscribers 534 subscribers
  • Views 679 views
  • Users 0 members are here
  • c
  • programming
  • string
Related

Converting formatted string to struct values

balajivan1995
balajivan1995 over 1 year ago

Currently I am working on GPS clock for the Time and Space Projects and I was going through my old repository to salvage few lines of code I wrote few years ago. In one of my earlier projects, I was working on parsing GPS data to obtain location coordinates and date/time values. After writing a regex like string parser that took nearly 50ms processing time. I thought there must be a better and faster way to parse a string that is in a predefined pattern. Since GPS data I receive is usually in csv format, I decided to check c/c++ implementation of .csv files and found a standard function called "sscanf". 

The full form of sscanf is string scanf, which basically means it accept and read string data as input, as opposed to regular scanf which takes in user fed data as input. Functionally, it does the opposite of sprintf. In sprintf, the values in variables get placed in predefined string format into a character array whereas in sscanf, formatted character array is matched with the predefined string format and the values will be stored in variable.

In the below code snippet I have created a simple function that parses the date & time present in a character array. As we know, timestamps usually follow the "yyyy-mm-dd hh:mm:dd", it is quite easy to create a string format that captures integer values and assign to variables. I have explicitly stated the digits with format specifier as %02 to make sure leading zero will be printed.

#include <stdio.h>
#include <stdint.h>
#include <string.h>

typedef struct
{
  int year;
  int month;
  int day;
  int hour;
  int minutes;
  int seconds;
} time_data;

int main ()
{
  char *timestamp = "2023-05-23 21:05:32";
  time_data tm_dt;
  char ouput_buffer[30];
  // note the '&' before the variable names, we are storing the values in address of variables like we do in scanf.
  sscanf (timestamp, "%04d-%02d-%02d %02d:%02d:%02d", &tm_dt.year,
		  &tm_dt.month, &tm_dt.day, &tm_dt.hour, &tm_dt.minutes,
		  &tm_dt.seconds);

  sprintf (ouput_buffer, "%04d-%02d-%02d %02d:%02d:%02d", tm_dt.year,
		   tm_dt.month, tm_dt.day, tm_dt.hour, tm_dt.minutes, tm_dt.seconds);

  printf ("%s", ouput_buffer);
  return 0;
}

Note : For known size & format of simple strings, this function will be quite useful. This code by no means is a production suitable code and  it is not advisable to use sscanf or most of the functions in stdio without checking the size first.

  • Sign in to reply
  • Cancel
Parents
  • michaelkellett
    michaelkellett over 1 year ago

    How fast (and how memory hungry) is this alternative ?

    (and what processor and software environment).

    MK

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • balajivan1995
    balajivan1995 over 1 year ago in reply to michaelkellett

    Not sure about the memory, but it is really fast. Less than 1ms with ESP32.Odd thing is, printing the values back took 1ms. This is much faster than the one I wrote earlier where I check character by character and split the string by delimiter, convert the obtained value to int.

    Here is the current implementation with sample randomized character string.

    image

    function definition

    image

    Function to print the values

    image

    The output

    image

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • michaelkellett
    michaelkellett over 1 year ago in reply to balajivan1995

    That's interesting - some of the standard C library functions (eg printf() use a lot of memory (code and data) by small embedded standards. I've never worried much about the timing because almost every project where I've used printf() it's outputting via a UART.

    Thanks for the info.

    MK

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
Reply
  • michaelkellett
    michaelkellett over 1 year ago in reply to balajivan1995

    That's interesting - some of the standard C library functions (eg printf() use a lot of memory (code and data) by small embedded standards. I've never worried much about the timing because almost every project where I've used printf() it's outputting via a UART.

    Thanks for the info.

    MK

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