Skip to content

Instantly share code, notes, and snippets.

@pedromenezes
Created January 27, 2011 20:45
Show Gist options
  • Save pedromenezes/799227 to your computer and use it in GitHub Desktop.
Save pedromenezes/799227 to your computer and use it in GitHub Desktop.
VALUE
rb_fix2str(VALUE x, int base)
{
extern const char ruby_digitmap[];
char buf[SIZEOF_VALUE*CHAR_BIT + 2], *b = buf + sizeof buf;
long val = FIX2LONG(x);
int neg = 0;
if (base < 2 || 36 < base) {
rb_raise(rb_eArgError, "invalid radix %d", base);
}
if (val == 0) {
return rb_usascii_str_new2("0");
}
if (val < 0) {
val = -val;
neg = 1;
}
*--b = '\0';
do {
*--b = ruby_digitmap[(int)(val % base)];
} while (val /= base);
if (neg) {
*--b = '-';
}
return rb_usascii_str_new2(b);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment