I once worked as a C-type conversion algorithm in college. At that time, due to technical limitations, the value size was limited (the system data type length was limited). Many years later, I felt like I was about to get rid of my learning machine. I sorted out some things and occasionally found this conversion algorithm in a corner. I think it has some improvements, it can make it more powerful (I thought about converting ultra-long numeric values at the time, but it is limited by the system data type, the deep cognition of personal development languages, and the narrow view of vision, failed to think of an effective way to solve the problem of ultra-long data computing). Here I will paste the section I wrote in college many years ago with my modified post to serve readers. Input parameter: String X is a massive string format. Of course, the larger the value, the more powerful this method is. Int y is to convert the input decimal number (the previous string) to other hexadecimal values within 32 bits. The range is (2 ~ 32) Note: It is said that ultra-long data is still the largest
The number limit will overflow, but the maximum number is really big. Isn't it born ?! Haha.
static string DecimalismConversion(string x, int y) { Stack stack = new Stack(); int length = Int64.MaxValue.ToString().Length - 2; while ( x!="") { string quotient = string.Empty; string remainder = string.Empty; int count = 0; Int64 i64 = 0; while (count * length < x.Length) { string str = x.Substring(count * length, Math.Min(length, x.Length - count * length)); i64 = Convert.ToInt64(remainder + str); remainder = (i64 % y).ToString(); quotient += (i64 / y).ToString().PadLeft(str.Length, ‘0‘); count++; } Int64.TryParse(remainder, out i64); if (i64 % y < 10) stack.Push((char)(i64 % y + 48)); else stack.Push((char)(i64 % y + 55)); x = quotient.TrimStart(new char[] { ‘ ‘, ‘0‘ }); } return new string(stack.ToArray()); }
The following section is its original body. Now it is ridiculous to think about it. It is so ugly to write a program design (C language) in the past.
int main(int x,int y) { int i=0; char a[17]; printf("Input:"); L: scanf("%d %d",&x,&y); if(x<=0||y<=2||y>=33) { printf("Input error,input again:"); goto L; } while(x!=0) { if(x%y<10) a=(char)(x%y+48); else a=(char)(x%y+55); x=x/y; } for(i=i-1;i>=0;i--) printf("%c",a); putchar(‘\n‘); }