Quickly convert Decimal to other bases in C#

王林
Release: 2023-08-27 09:41:05
forward
1017 people have browsed it

Quickly convert Decimal to other bases in C#

To quickly convert decimal to other bases, use the stack. Let's look at an example.

First, I set the variable "baseNum" to 2

int baseNum = 2;
Copy after login

Similarly, if you want another base, then -

// base 8
int baseNum = 8;

// base 10
int baseNum = 10;
Copy after login

After getting the value, set one Stack, the value is calculated by finding the remainder, as shown below.

Here, n is a decimal number.

Stack s = new Stack();
do {
   s.Push(n % baseNum);
   n /= baseNum;
} while (n != 0);
Copy after login

After using the stack, pop the element. This will give you the results.

Assuming the number n is 45, then the binary result (i.e. in base 2) will be -

Result...
101101
Copy after login

The above is the detailed content of Quickly convert Decimal to other bases in C#. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template