diff --git a/pack.cpp b/pack.cpp index 8c9a876..48d495b 100644 --- a/pack.cpp +++ b/pack.cpp @@ -71,58 +71,6 @@ void pack3_6bit(uint32_t nc1, uint32_t nc2, uint16_t ng, uint8_t *payload) { } -// Parse a 2 digit integer from string -int dd_to_int(const char *str, int length) { - int result = 0; - bool negative; - int i; - if (str[0] == '-') { - negative = true; - i = 1; // Consume the - sign - } - else { - negative = false; - i = (str[0] == '+') ? 1 : 0; // Consume a + sign if found - } - - while (i < length) { - if (str[i] == 0) break; - if (!is_digit(str[i])) break; - result *= 10; - result += (str[i] - '0'); - ++i; - } - - return negative ? -result : result; -} - - -// Convert a 2 digit integer to string -void int_to_dd(char *str, int value, int width) { - if (value < 0) { - *str = '-'; - ++str; - value = -value; - } - - int divisor = 1; - for (int i = 0; i < width; ++i) { - divisor *= 10; - } - - while (divisor > 1) { - int digit = value / divisor; - - *str = '0' + digit; - ++str; - - value -= digit * divisor; - divisor /= 10; - } - *str = 0; // Add zero terminator -} - - // Pack a valid callsign into a 28-bit integer. // Note that callsign points to a portion of text and may not be zero-terminated. int32_t packcall(const char *callsign, int length) { diff --git a/text.cpp b/text.cpp index 4e0d10d..cc446d0 100644 --- a/text.cpp +++ b/text.cpp @@ -49,3 +49,55 @@ void fmtmsg(char *msg_out, const char *msg_in) { } *msg_out = 0; // Add zero termination } + + +// Parse a 2 digit integer from string +int dd_to_int(const char *str, int length) { + int result = 0; + bool negative; + int i; + if (str[0] == '-') { + negative = true; + i = 1; // Consume the - sign + } + else { + negative = false; + i = (str[0] == '+') ? 1 : 0; // Consume a + sign if found + } + + while (i < length) { + if (str[i] == 0) break; + if (!is_digit(str[i])) break; + result *= 10; + result += (str[i] - '0'); + ++i; + } + + return negative ? -result : result; +} + + +// Convert a 2 digit integer to string +void int_to_dd(char *str, int value, int width) { + if (value < 0) { + *str = '-'; + ++str; + value = -value; + } + + int divisor = 1; + for (int i = 0; i < width; ++i) { + divisor *= 10; + } + + while (divisor > 1) { + int digit = value / divisor; + + *str = '0' + digit; + ++str; + + value -= digit * divisor; + divisor /= 10; + } + *str = 0; // Add zero terminator +} \ No newline at end of file diff --git a/text.h b/text.h index 0d904d4..2891668 100644 --- a/text.h +++ b/text.h @@ -12,3 +12,9 @@ bool equals(const char *string1, const char *string2); // - replaces lowercase letters with uppercase // - merges consecutive spaces into single space void fmtmsg(char *msg_out, const char *msg_in); + +// Parse a 2 digit integer from string +int dd_to_int(const char *str, int length); + +// Convert a 2 digit integer to string +void int_to_dd(char *str, int value, int width);