Search Unity

Parsing data

Discussion in 'Scripting' started by Adrianoooz, Jan 17, 2019.

  1. Adrianoooz

    Adrianoooz

    Joined:
    Jan 15, 2019
    Posts:
    12
    Hello everyone,

    So im parsing TLE data, for instance i got a line: "88109B", "88" means that the satellite was de-orbited at 1988. How can i write a short program that would change the first two characters to given date.
    Also if the string is something like this:"05108B", it means that the satellite was de-orbited at 2005, not 1905.

    Can anyone help me?

    Thanks alot !
     
  2. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    Code (csharp):
    1.  
    2. public static int ParseYearTLE(string data)
    3. {
    4.     int inCentury;
    5.     if (int.TryParse(data.Substring(0, 2), out inCentury))
    6.     {
    7.         if (inCentury < 0) return -1;
    8.         int century = inCentury <= 19 ? 20 : 19; // Up to 19 assume the 20th century, else the 19th century
    9.         int year = 100 * century + inCentury;
    10.         return year;
    11.     }
    12.     return -1;
    13. }
    14.  
    This would return the year as int. You could change that to a DateTime, but that seems more specific than the data contains. If there is a parsing error, it will return -1.
     
    craftingheroes and Adrianoooz like this.
  3. Adrianoooz

    Adrianoooz

    Joined:
    Jan 15, 2019
    Posts:
    12
    Thank you!