对于学校作业,我需要在C#中以十进制、十六进制、八进制和二进制数显示“约书亚”的每个字符的值。请帮帮我!
到目前为止,我只能展示我的名字.
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();
}
}
}发布于 2014-02-12 21:53:06
如果你不介意我建议你完全改变你的方法,我下面有一个解决方案。
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行代码对其进行循环,以完成转换和打印。
发布于 2014-02-12 23:30:51
一些提示,假设这个练习的目的是教你一些小技巧。
string本质上是一个char序列。char在封面下是一个16位无符号整数(ushort).一旦你有了这个积分值,转换到一个不同基的文本表示是相当简单的。转换为二进制,八进制或十六进制,因为这些都是2的幂基只是一个比特移动和掩蔽的问题。转换为小数点(基数10)涉及除法。
以下是一些尽量减少使用内置转换/格式化实用程序的示例。
转换为十六进制(基本16)。十六进制是表示二进制值的方便的缩写。它采用4组位('nibbles'),因此每个十六进制“数字”的范围为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个值):
private static string CharToOctalString( char c )
{
StringBuilder sb = new StringBuilder();
int codePoint = (ushort) c ;
int shiftAmount = 18 ; // has to be integral multiple of nibble size
do
{
shiftAmount -= 3 ;
// shift the value the correct number of bits to the right and mask off everthing but the low order nibble
int nibble = (codePoint>>shiftAmount)&0x0007 ;
sb.Append( "01234567"[nibble] ) ;
} while ( shiftAmount > 0 ) ;
string value = sb.ToString() ;
return value ;
}同样,将转换为二进制(基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的幂。这意味着我们必须用除法从低阶数字开始,用逆序剥离数字。为此,我们将使用Stack<T>,因为堆栈是LIFO (先入先出)数据结构,这将提供我们需要的反转质量。为此目的:
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 ;
}https://stackoverflow.com/questions/21740363
复制相似问题