任意数据的大小端转换

8个字节的长度限制需要注意。

#include 

typedef unsigned long  u64;

void convert_func(unsigned long val)
{
  //unsigned int a = 0, b = 0, c = 0, d = 0;
  unsigned long i = 0,j = 0;
  unsigned long length = 0,h_length = 0;
  unsigned long result = 0,result1 = 0,result2 = 0;
  unsigned long byte = 0,temp_byte = 0,temp = 0;


  length  = sizeof(val);
  h_length= length/2;

  for(i=0;i> i*8;
    result1 |= byte << (length-i-1)*8;
  }

  for(i=0;i> (length - i-1)*8;
    result2 |= byte << i*8;
  }
  result = result1 | result2;

  for(i=1;i>= i*8;
      break;
    }
  }
  printf("result = %lx\r\n",result);
}


int main()
{
    unsigned long byte = 0;
    while(1)
    {
      scanf("%lx",&byte);
      convert_func(byte);
    }
    return 0;
}

你可能感兴趣的:(C)