名称到十进制到二进制算法

本文关键字:算法 二进制 十进制 | 更新日期: 2023-09-27 18:07:00

我想做一个程序,问你关于你的名字,之后它显示你的名字的每个字符的数量根据ASCII表。现在我想把每个数字都转换成二进制和....我被卡住了!:)请帮帮我!下面是我的代码:

using System;

namespace DecimalToBinary
{
   class DecimalToBinary
   {
       static void Main()
       {
           Console.WriteLine("Pleasse, enter your name:");
           string name = Console.ReadLine();
           foreach (char ch in name)
           {
            Console.WriteLine("Character {0} is {1} according to ASCII      table.", ch, (int)ch);                
           }
           foreach (char ch in name)
           {
              int decNumber = ch;
              Console.WriteLine(decNumber);                
           }
           Console.WriteLine("Enter a number:");
           int number = Int32.Parse(Console.ReadLine());
           string binary = Convert.ToString(number, 2);
           Console.WriteLine(binary);             
     }
  }
}

名称到十进制到二进制算法

您的意思是将每个ASCII值转换为二进制和简单整数吗?

foreach (char ch in name)
{
    int decNumber = ch;
    Console.WriteLine(decNumber);                
    for (int i = 0; i < name.Length; i++)
    {
        Console.WriteLine("Enter a number:");
        int a = Convert.ToInt32(Console.ReadLine());
        var result = Convert.ToString(a, 2);
        Console.WriteLine(result);
    }             
}