Skip to content

Instantly share code, notes, and snippets.

@rpgmaker
Created November 15, 2013 05:09
Show Gist options
  • Save rpgmaker/7479398 to your computer and use it in GitHub Desktop.
Save rpgmaker/7479398 to your computer and use it in GitHub Desktop.
Int to string with goto
unsafe static string itoa_int32(int snum) {
char* s = stackalloc char[12];
char* ps = s;
int num1 = snum, num2, num3, div;
if (snum < 0) {
*ps++ = '-';
//Can't negate int min
if (snum == -2147483648)
return "-2147483648";
num1 = -num1;
}
if (num1 < 10000) {
if (num1 < 10) goto L1;
if (num1 < 100) goto L2;
if (num1 < 1000) goto L3;
} else {
num2 = num1 / 10000;
num1 -= num2 * 10000;
if (num2 < 10000) {
if (num2 < 10) goto L5;
if (num2 < 100) goto L6;
if (num2 < 1000) goto L7;
} else {
num3 = num2 / 10000;
num2 -= num3 * 10000;
if (num3 >= 10) {
*ps++ = (char)('0' + (char)(div = (num3 * 6554) >> 16));
num3 -= div * 10;
}
*ps++ = (char)('0' + (num3));
}
*ps++ = (char)('0' + (div = (num2 * 8389) >> 23));
num2 -= div * 1000;
L7:
*ps++ = (char)('0' + (div = (num2 * 5243) >> 19));
num2 -= div * 100;
L6:
*ps++ = (char)('0' + (div = (num2 * 6554) >> 16));
num2 -= div * 10;
L5:
*ps++ = (char)('0' + (num2));
}
*ps++ = (char)('0' + (div = (num1 * 8389) >> 23));
num1 -= div * 1000;
L3:
*ps++ = (char)('0' + (div = (num1 * 5243) >> 19));
num1 -= div * 100;
L2:
*ps++ = (char)('0' + (div = (num1 * 6554) >> 16));
num1 -= div * 10;
L1:
*ps++ = (char)('0' + (num1));
return new string(s);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment