0

Using Microsoft's implementation of itoa, how do you get "a7ffda89" as the base 17 representation of -9 in base 10? What I am looking for is a description of the algorithm.

Here is the code I used to find out what itoa would return using Microsoft's implementation:

#include "stdafx.h"  
#include <stdlib.h>

int main(int argc, char *argv[])
{
  (void)argc;
  (void)argv;
  char buff[256] = {};
  memset(buff, '0', 256);
  _itoa_s(-9, buff, 256, 17);
  exit(0);
}
Matthew Hoggan
  • 7,402
  • 16
  • 75
  • 140

2 Answers2

3

From cplusplus.com:

If base is 10 and value is negative, the resulting string is preceded with a minus sign (-). With any other base, value is always considered unsigned.

Cornstalks
  • 37,137
  • 18
  • 79
  • 144
2

-9 expressed as an unsigned 32-bit integer is 4294967287. Converting that to base 17 gives a7ffda89. As a check, in Python:

>>> int('a7ffda89', 17) - (1<<32)
-9
nneonneo
  • 171,345
  • 36
  • 312
  • 383