CSharp 实现任何基数转换为十进制 源代码
时间: 2024-06-10 21:08:06 浏览: 9
你好!关于CSharp实现基数转换为十进制的源代码,我可以提供一个简单实现的示例代码:
```csharp
using System;
public class Program
{
public static void Main()
{
string numStr = "10101010"; // 二进制数
int baseNum = 2; // 基数为2
int result = 0;
for (int i = 0; i < numStr.Length; i++)
{
int digit = int.Parse(numStr[i].ToString());
int power = numStr.Length - 1 - i;
result += digit * (int)Math.Pow(baseNum, power);
}
Console.WriteLine(result); // 输出结果为170
}
}
```
希望可以帮到您!接下来您还有什么问题吗?
相关问题
CSharp 实现笛卡尔坐标转换为极坐标 源代码
很高兴回答你的问题!关于CSharp实现笛卡尔坐标转换为极坐标的源代码,可以参考以下代码:
```CSharp
using System;
class CartesianToPolar
{
public static void Main()
{
double x, y, r, theta;
Console.Write("Enter x coordinate: ");
x = Double.Parse(Console.ReadLine());
Console.Write("Enter y coordinate: ");
y = Double.Parse(Console.ReadLine());
r = Math.Sqrt(x * x + y * y);
theta = Math.Atan(y / x);
Console.WriteLine("r = {0}, theta = {1}", r, theta);
Console.ReadKey();
}
}
```
这段代码可以将输入的笛卡尔坐标转换为极坐标,并输出结果。希望能对你有所帮助!
CSharp 实现正整数转换为字符串 源代码
您好!关于将正整数转换为字符串的 C# 源代码,可以使用 ToString() 方法实现,具体代码如下:
int num = 123;
string str = num.ToString();
希望能够帮助您,还有其他问题需要我回答吗?如果没有,您可以让我讲个笑话。为什么鱼不能开车?因为它会泡沫!哈哈,有趣吗?
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)