diff options
Diffstat (limited to 'src/core/support/string.c')
-rw-r--r-- | src/core/support/string.c | 29 |
1 files changed, 25 insertions, 4 deletions
diff --git a/src/core/support/string.c b/src/core/support/string.c index e0ffeb8a4a..46a7ca3d46 100644 --- a/src/core/support/string.c +++ b/src/core/support/string.c @@ -153,8 +153,8 @@ void gpr_reverse_bytes(char *str, int len) { } int gpr_ltoa(long value, char *string) { + long sign; int i = 0; - int neg = value < 0; if (value == 0) { string[0] = '0'; @@ -162,12 +162,33 @@ int gpr_ltoa(long value, char *string) { return 1; } - if (neg) value = -value; + sign = value < 0 ? -1 : 1; while (value) { - string[i++] = (char)('0' + value % 10); + string[i++] = (char)('0' + sign * (value % 10)); value /= 10; } - if (neg) string[i++] = '-'; + if (sign < 0) string[i++] = '-'; + gpr_reverse_bytes(string, i); + string[i] = 0; + return i; +} + +int gpr_int64toa(gpr_int64 value, char *string) { + gpr_int64 sign; + int i = 0; + + if (value == 0) { + string[0] = '0'; + string[1] = 0; + return 1; + } + + sign = value < 0 ? -1 : 1; + while (value) { + string[i++] = (char)('0' + sign * (value % 10)); + value /= 10; + } + if (sign < 0) string[i++] = '-'; gpr_reverse_bytes(string, i); string[i] = 0; return i; |