Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 显示字母、十进制、十六进制、八进制、二进制_C#_Binary_Decimal_Octal - Fatal编程技术网

C# 显示字母、十进制、十六进制、八进制、二进制

C# 显示字母、十进制、十六进制、八进制、二进制,c#,binary,decimal,octal,C#,Binary,Decimal,Octal,我需要以十进制、十六进制、八进制和二进制C#显示“Joshua”的每个字符的值,以供学校作业使用。请帮忙 到目前为止,我只能显示我的名字 namespace CharFormat { class Program { static void Main(string[] args) { char letter1; letter1 = 'J'; System.Console.Writ

我需要以十进制、十六进制、八进制和二进制C#显示“Joshua”的每个字符的值,以供学校作业使用。请帮忙

到目前为止,我只能显示我的名字

namespace CharFormat
{
    class Program
    {
        static void Main(string[] args)
        {
            char letter1;
            letter1 = 'J';
            System.Console.Write(letter1);
            char letter2;
            letter2 = 'o';
            System.Console.Write(letter2);
            char letter3;
            letter3 = 's';
            System.Console.Write(letter3);
            char letter4;
            letter4 = 'h';
            System.Console.Write(letter4);
            char letter5;
            letter5 = 'u';
            System.Console.Write(letter5);
            char letter6;
            letter6 = 'a';
            System.Console.Write(letter6);
            System.Console.ReadLine();
        }
    }
}

如果你不介意我建议你完全改变你的方法,我有一个解决方案

string name = "Joshua";
char[] characters = name.ToCharArray(); //many ways to do this, just first that comes to mind

foreach (char c in characters)
{
    Console.WriteLine("Decimal: " + Convert.ToByte(c).ToString());
    Console.WriteLine("Character: " + c.ToString());
    Console.WriteLine("Other representation: " + Convert.YouFigureThisPartOut(c).ToString());

    //ect
}

其基本思想是,不要编写50条if语句,而是将字符放入数组中,使用相同的4或5行代码在其上循环进行转换和打印。

一些提示,假设此练习的目的是教您一些技巧

  • string
    本质上是一个
    char
    序列
  • char
    实际上是一个16位无符号整数(
    ushort
一旦你有了积分值,转换成不同基的[文本表示]就相当简单了。转换为二进制、八进制或十六进制,因为这些都是2的幂基,所以只需移位和掩蔽即可。转换为十进制(以10为基数)需要除法

下面是一些尽量少使用内置转换/格式化实用程序的示例

转换为十六进制(基数16)。十六进制是表示二进制值的一种方便的简写形式。它以4(“半字节”)为一组,因此每个十六进制“数字”的范围为16个值(0-15)。转换是以4为单位的移位和掩蔽。由于我们在移动和掩蔽,我们可以按顺序构建结果:

private static string CharToHexString( char c )
{
  StringBuilder sb = new StringBuilder();
  int codePoint = (ushort) c ;
  int shiftAmount = 16 ;
  do
  {
    shiftAmount -= 4 ;
    // shift the value the correct number of bits to the right and mask off everthing but the low order nibble
    int nibble = (codePoint>>shiftAmount)&0x000F ;
    sb.Append( "0123456789ABCDEF"[nibble] ) ;
  } while ( shiftAmount > 0 ) ;
  string value = sb.ToString() ;
  return value ;
}
转换为八进制(基数8)基本上与转换为十六进制相同。不同之处在于半字节大小为3位,因此数字的域为0-7(8个值):

转换为二进制(基2)同样,基本上与上述相同,半字节大小为1位:

private static string CharToBinaryString( char c )
{
  StringBuilder sb = new StringBuilder();
  int codePoint = (ushort) c ;
  int shiftAmount = 16 ;
  do
  {
    shiftAmount -= 1 ;
    // shift the value the correct number of bits to the right and mask off everything but the low order nibble
    int nibble = (codePoint>>shiftAmount)&0x0001 ;
    sb.Append( "01"[nibble] ) ;
  } while ( shiftAmount > 0 ) ;
  string value = sb.ToString() ;
  return value ;
}
转换为十进制(以10为基数)。这有点不同,因为基数不是2的幂。这意味着我们必须使用除法以相反的顺序剥离数字,从低阶数字开始。为此,我们将使用
堆栈
,因为堆栈是后进先出(last-in,first-out,后进先出)数据结构,这将提供我们需要的反转质量。为此:

private static string CharToDecimalString( char c )
{
  Stack<char> digits = new Stack<char>() ;
  int codePoint = (ushort) c ;
  do
  {
    int digit = codePoint % 10 ; // modulo 10 arithmetic gives us the low order digit
    codePoint = codePoint / 10 ; // integer division by 10 shifts the lower order digit off
    digits.Push("0123456789"[digit]);
  } while ( codePoint > 0 ) ;

  string value = new string( digits.ToArray() ) ; // this pops the entire stack, thus reversing the digits.
  return value ;
}
私有静态字符串CharToDecimalString(charc)
{
堆栈位数=新堆栈();
int代码点=(ushort)c;
做
{
int digit=codePoint%10;//模10算法给出低位数字
codePoint=codePoint/10;//整数除以10将低位数字移位
数字。按下(“0123456789”[数字]);
}而(码点>0);
字符串值=新字符串(digits.ToArray());//这将弹出整个堆栈,从而反转数字。
返回值;
}

您将收到很多“发布您尝试过的内容,否则我们不会在这里帮助您”。首先,你的问题是什么?你在哪里卡住了?我希望你不要指望我们给你写作业。首先,你知道他们之间的区别吗?你可以使用吗?这让我很清楚我需要做什么来完成我的作业。非常感谢!完成作业后,我会发布我的最终结果。
private static string CharToDecimalString( char c )
{
  Stack<char> digits = new Stack<char>() ;
  int codePoint = (ushort) c ;
  do
  {
    int digit = codePoint % 10 ; // modulo 10 arithmetic gives us the low order digit
    codePoint = codePoint / 10 ; // integer division by 10 shifts the lower order digit off
    digits.Push("0123456789"[digit]);
  } while ( codePoint > 0 ) ;

  string value = new string( digits.ToArray() ) ; // this pops the entire stack, thus reversing the digits.
  return value ;
}