unsigned int iTemp =0;
printf("Get the number of the byte?");
int nByte;
scanf("%d",&nByte);
unsigned int byte[100];
for(int i = 0;i<nByte;i++)
{
printf("Get the byte No.%d",i);
scanf("%02x",&byte[i]);
}
for(i=0;i<nByte;i++)
{
iTemp ^= (byte[i]<<8);
for(int j=0;j<8;j++)
{
int flag = iTemp & 0x8000;
iTemp <<= 1;
if(flag)
{
iTemp ^= 0x1021;
}
}
}
printf("The number is %04x\r\n",iTemp);

解决方案 »

  1.   


    int   iTemp = 0; 
    Console.WriteLine("Get   the   number   of   the   byte?"); 
    int   nByte; 
    nByte = int.Parse(Console.ReadLine());
    int[]   bt = new int[100]; 
    for(int i = 0;i <nByte;i++) 

    Console.WriteLine("Get   the   byte   No.{0}",i); 
    bt[i] = int.Parse(Console.ReadLine());

    for(int i=0;i <nByte;i++) 

    iTemp ^= (bt[i] <<8); 
    for(int   j=0;j <8;j++) 

    int flag   =   iTemp   &   0x8000; 
    iTemp <<= 1; 
    if(flag != 0) 

    iTemp   ^=   0x1021; 



    Console.WriteLine("The   number   is   {0:X}",iTemp);
      

  2.   

    没有进行错误处理,自己处理,输入的时候 int   iTemp = 0; 
    Console.WriteLine("Get   the   number   of   the   byte?"); 
    int   nByte; 
    nByte = int.Parse(Console.ReadLine());
    int[]   bt = new int[100]; 
    for(int i = 0;i <nByte;i++) 

    Console.WriteLine("Get   the   byte   No.{0}",i); 
    bt[i] = int.Parse(Console.ReadLine());

    for(int i=0;i <nByte;i++) 

    iTemp ^= (bt[i] <<8); 
    for(int   j=0;j <8;j++) 

    int flag   =   iTemp   &   0x8000; 
    iTemp <<= 1; 
    if(flag != 0) 

    iTemp   ^=   0x1021; 



    Console.WriteLine("The   number   is   {0:X}",iTemp);
      

  3.   

    unsigned int => uint
    printf => Console.WriteLine
    scanf => Console.ReadLine