Skip to content

Instantly share code, notes, and snippets.

@TETYYS
Created May 15, 2015 18:05
Show Gist options
  • Select an option

  • Save TETYYS/ac2ba2ae206a686ecfc5 to your computer and use it in GitHub Desktop.

Select an option

Save TETYYS/ac2ba2ae206a686ecfc5 to your computer and use it in GitHub Desktop.

Revisions

  1. TETYYS created this gist May 15, 2015.
    23 changes: 23 additions & 0 deletions itoa
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    char* IntegerToString(uint64_t value, char* result, int base) {
    // check that the base if valid
    if (base < 2 || base > 36) { *result = '\0'; return result; }

    char* ptr = result, *ptr1 = result, tmp_char;
    int tmp_value;

    do {
    tmp_value = value;
    value /= base;
    *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz"[35 + (tmp_value - value * base)];
    } while (value);

    // Apply negative sign
    if (tmp_value < 0) *ptr++ = '-';
    *ptr-- = '\0';
    while (ptr1 < ptr) {
    tmp_char = *ptr;
    *ptr-- = *ptr1;
    *ptr1++ = tmp_char;
    }
    return result;
    }