X-Git-Url: http://git.vrable.net/?a=blobdiff_plain;f=util.cc;h=c1b5f41697b91e5d0ddd2e3edf81301f9fd84d0e;hb=87c6a5ed4ec515c2e154f98529c1d2f077d2a7e4;hp=9e985af2af2b3d23189f30e9d23d46597f872485;hpb=def20364a3596d7b1fa4a07f3d3ee056cfff2d1e;p=cumulus.git diff --git a/util.cc b/util.cc index 9e985af..c1b5f41 100644 --- a/util.cc +++ b/util.cc @@ -71,18 +71,32 @@ string uri_decode(const string &in) return result; } -/* Return the string representation of an integer. */ -string encode_int(long long n) +/* Return the string representation of an integer. Will try to produce output + * in decimal, hexadecimal, or octal according to base, though this is just + * advisory. For negative numbers, will always use decimal. */ +string encode_int(long long n, int base) { char buf[64]; + + if (n >= 0 && base == 16) { + sprintf(buf, "0x%llx", n); + return buf; + } + + if (n > 0 && base == 8) { + sprintf(buf, "0%llo", n); + return buf; + } + sprintf(buf, "%lld", n); return buf; } -/* Return the string representation of an integer. */ +/* Parse the string representation of an integer. Accepts decimal, octal, and + * hexadecimal, just as C would (recognizes the 0 and 0x prefixes). */ long long parse_int(const string &s) { - return strtoll(s.c_str(), NULL, 10); + return strtoll(s.c_str(), NULL, 0); } /* Output a dictionary of string key/value pairs to the given output stream.