Adding leading zeros to variable data - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Adding leading zeros to variable data

ve6sarve6sar Posts: 11Member
I'm sure there's a simple solution to my problem but not programing full time I'm not as learned as I'd like to be. I'm working on a script that grabs Airport weather data using Geo::METAR and formats it into a telemetry packet. Now a few of the fields are outputed from Geo::METAR in variable lengths because of changing weather conditions ie. 5 or 20 Mile per hour wind speeds. what I need to do is add leading zeros on so I have three bytes in the field once the script outputs the data ie 005 or 020.
Like I said I'm sure there's an easy way to do it but it escapes me..

My out put packet currently looks like this:
CYPE>APRS,TCPIP*,qAC,APRSFL:@100232z5614.00N/11726.00W_160/[b]3[/b]g...t057b10172

It should look like this:
CYPE>APRS,TCPIP*,qAC,APRSFL:@100232z5614.00N/11726.00W_160/[b]003[/b]g...t057b10172
Sean

Comments

  • ndixonndixon Posts: 5Member
    : I'm sure there's a simple solution to my problem but not programing
    : full time I'm not as learned as I'd like to be. I'm working on a
    : script that grabs Airport weather data using Geo::METAR and formats
    : it into a telemetry packet. Now a few of the fields are outputed
    : from Geo::METAR in variable lengths because of changing weather
    : conditions ie. 5 or 20 Mile per hour wind speeds. what I need to do
    : is add leading zeros on so I have three bytes in the field once the
    : script outputs the data ie 005 or 020.
    : Like I said I'm sure there's an easy way to do it but it escapes me..
    :
    : My out put packet currently looks like this:
    : CYPE>APRS,TCPIP*,qAC,APRSFL:@100232z5614.00N/11726.00W_160/[b]3[/b]g.
    : ..t057b10172
    :
    : It should look like this:
    : CYPE>APRS,TCPIP*,qAC,APRSFL:@100232z5614.00N/11726.00W_160/[b]003[/b]
    : g...t057b10172
    : Sean
    :

    You can format a number with leading zeros using [b]sprintf()[/b]
    e.g.

    $n = 3;
    $sn = sprintf("%03d", $n);

    Try [italic]perldoc -f sprintf[/italic] for more on this function
  • ve6sarve6sar Posts: 11Member
    : : I'm sure there's a simple solution to my problem but not programing
    : : full time I'm not as learned as I'd like to be. I'm working on a
    : : script that grabs Airport weather data using Geo::METAR and formats
    : : it into a telemetry packet. Now a few of the fields are outputed
    : : from Geo::METAR in variable lengths because of changing weather
    : : conditions ie. 5 or 20 Mile per hour wind speeds. what I need to do
    : : is add leading zeros on so I have three bytes in the field once the
    : : script outputs the data ie 005 or 020.
    : : Like I said I'm sure there's an easy way to do it but it escapes me..
    : :
    : : My out put packet currently looks like this:
    : : CYPE>APRS,TCPIP*,qAC,APRSFL:@100232z5614.00N/11726.00W_160/[b]3[/b]g.
    : : ..t057b10172
    : :
    : : It should look like this:
    : : CYPE>APRS,TCPIP*,qAC,APRSFL:@100232z5614.00N/11726.00W_160/[b]003[/b]
    : : g...t057b10172
    : : Sean
    : :
    :
    : You can format a number with leading zeros using [b]sprintf()[/b]
    : e.g.
    :
    : $n = 3;
    : $sn = sprintf("%03d", $n);
    :
    : Try [italic]perldoc -f sprintf[/italic] for more on this function

    Thanks that did the trick.

    Sean

Sign In or Register to comment.