C # hexadecimal conversion (no numerical length limit)

Source: Internet
Author: User
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‘); }

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.